Skip to main content

Kutools for Office β€” One Suite. Five Tools. Get More Done.

How to auto remove some special characters from subject line when sending emails?

Author Xiaoyang Last modified

You may insert some special characters accidentally when you type subject in the subject line while sending email in Outlook, so you need to remove these special characters one by one. In this article, I will talk about how to remove all these special characters automatically from subject when sending emails.

Remove all special characters from subject line automatically when sending emails with VBA code


Remove all special characters from subject line automatically when sending emails with VBA code

Auto remove the special characters from subject line when sending emails, the following VBA code can do you a favor. Please do as this:

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, double click ThisOutlookSession from the Project1(VbaProject.OTM) pane to open the mode, and then copy and paste the following code into the blank module.

VBA code: Auto remove special characters from subject line

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    Dim xMailItem As Outlook.MailItem
    Dim xRegExp As RegExp
    Dim xSubject As String
    On Error Resume Next
    If Item.Class <> olMail Then Exit Sub
    Set xMailItem = Item
    xSubject = xMailItem.Subject
    Set xRegExp = CreateObject("VBScript.RegExp")
    With xRegExp
        .Global = True
        .IgnoreCase = True
        .Pattern = "[^a-zA-Z0-9\u4e00-\u9fa5]"
    End With
    If xRegExp.test(xSubject) = False Then Exit Sub
    xSubject = xRegExp.Replace(xSubject, "-")
    xMailItem.Subject = GetTargetStr(xSubject)
    Set RegExp = Nothing
End Sub
Function GetTargetStr(Str As String)
    Dim xS, xStr As String
    Dim i As Integer
    Dim xIsFirst As Boolean
    xIsFirst = True
    xStr = ""
    For i = 1 To Len(Str)
        xS = Mid(Str, i, 1)
        If xS = "-" Then
            If xIsFirst Then
                xS = " "
                xIsFirst = False
            Else
                xS = ""
            End If
        Else
            xIsFirst = True
        End If
        xStr = xStr + xS
    Next i
    GetTargetStr = xStr
End Function
doc remove special char 1

3. And then, still in the Microsoft Visual Basic for Applications window, click Tools > References to go to the References-Project1 dialog box, and check Microsoft VBAScript Regular Expressions 1.0 option from the Available References list box, see screenshot:

doc remove special char 2

4. Then click OK to close the dialog, now, when you send a message with some special characters in the subject line, it will be removed automatically after sending it. See screenshot:

doc remove special char 3

Best Office Productivity Tools

Breaking News: Kutools for Outlook Launches Free Version!

Experience the all-new Kutools for Outlook with 100+ incredible features! Click to download now!

πŸ€– Kutools AI : Uses advanced AI technology to handle emails effortlessly, including replying, summarizing, optimizing, extending, translating, and composing emails.

πŸ“§ Email Automation: Auto Reply (Available for POP and IMAP)  /  Schedule Send Emails  /  Auto CC/BCC by Rules When Sending Email  /  Auto Forward (Advanced Rules)   /  Auto Add Greeting   /  Automatically Split Multi-Recipient Emails into Individual Messages ...

πŸ“¨ Email Management: Recall Emails  /  Block Scam Emails by Subjects and Others  /  Delete Duplicate Emails  /  Advanced Search  /  Consolidate Folders ...

πŸ“ Attachments ProBatch Save  /  Batch Detach  /  Batch Compress  /  Auto Save   /  Auto Detach  /  Auto Compress ...

🌟 Interface Magic: 😊More Pretty and Cool Emojis   /  Remind you when important emails come  /  Minimize Outlook Instead of Closing ...

πŸ‘ One-click Wonders: Reply All with Attachments  /   Anti-Phishing Emails  /  πŸ•˜Show Sender's Time Zone ...

πŸ‘©πŸΌβ€πŸ€β€πŸ‘©πŸ» Contacts & Calendar: Batch Add Contacts From Selected Emails  /  Split a Contact Group to Individual Groups  /  Remove Birthday Reminders ...

Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Instantly unlock Kutools for Outlook with a single click. Don't wait, download now and boost your efficiency!

kutools for outlook features1 kutools for outlook features2

πŸš€ One-Click Download β€” Get All Office Add-ins

Strongly Recommended: Kutools for Office (5-in-1)

One click to download five installers at once β€” Kutools for Excel, Outlook, Word, PowerPoint and Office Tab Pro. Click to download now!

  • βœ… One-click convenience: Download all five setup packages in a single action.
  • πŸš€ Ready for any Office task: Install the add-ins you need, when you need them.
  • 🧰 Included: Kutools for Excel / Kutools for Outlook / Kutools for Word / Office Tab Pro / Kutools for PowerPoint