Removing illegal characters

Status
Not open for further replies.

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
HI, I'm using the code below to remove illegal characters before saving an email in MSG format. At present the illegal characters are either replaced with a hyphen or an underscore. I would like to amend this to simply remove the illegal characters and not substitute e.g. hel&p would become help rather than hel-p. However when I replace "_" in the code below with "" I get an error?
Code:
  ' Clean out characters from Subject which are not permitted in a file name

   For intC = 1 To Len(strMsgSubj)
     If InStr(1, ":<&>""", Mid(strMsgSubj, intC, 1)) > 0 Then
       Mid(strMsgSubj, intC, 1) = "-"
     End If
   Next intC

   For intC = 1 To Len(strMsgSubj)
     If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
       Mid(strMsgSubj, intC, 1) = "_"
     End If
   Next intC
 

niton

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
If you just want results rather than the technical reason Mid does not delete try this.

Private Sub remove_illegal()

' Clean out characters from Subject which are not permitted in a file name
' Loop in reverse if deleting

Dim strMsgSubj As String
Dim intC As Long

strMsgSubj = "Hello :<&>Jack\/|*? Goodbye"
Debug.Print strMsgSubj

For intC = Len(strMsgSubj) To 1 Step -1
If InStr(1, ":<&>""", Mid(strMsgSubj, intC, 1)) > 0 Then
strMsgSubj = Replace(strMsgSubj, (Mid(strMsgSubj, intC, 1)), "")
End If
Next intC

For intC = Len(strMsgSubj) To 1 Step -1
If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
strMsgSubj = Replace(strMsgSubj, (Mid(strMsgSubj, intC, 1)), "")
End If
Next intC

Debug.Print strMsgSubj
End Sub
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Great thanks. I'll happily just accept the results.
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
R Outlook is NOT removing attachments. Using Outlook 4
M Help sending email but removing signature via VBA Outlook VBA and Custom Forms 5
B Removing Recipients from an automatic response Outlook VBA and Custom Forms 2
O How to paste website content using a specific font and removing URLs Using Outlook 2
M VBA Rule for removing all body but hyperlink then forwarding Outlook VBA and Custom Forms 9
N Recurring invite sent w/distribution list adding/removing attendees Using Outlook 0
D 'removing' an e-mail folder Using Outlook 2
G Wrong display name in from column when removing pf replication Exchange Server Administration 1
S Removing an Exchange Server account, retaining POP accounts Exchange Server Administration 1
D Removing Pop3 accounts on mulitple computers Using Outlook 3
K Removing outlines from outlook calendar Using Outlook 1
L Outlook 2007 - How to remove account without removing mail folders? Using Outlook 4
R What to backup before removing Outlook 2010? Using Outlook 4
V Outlook problem after removing avast Using Outlook 1
P Removing expired e-mail addresses Using Outlook 2
D Removing multiple Exchange 2007 servers on the same Domain and site Exchange Server Administration 1
S Removing myself as delegate from someone else's calendar Using Outlook 1
T Removing time zone setting from an appointment / meeting: is it possible? Using Outlook 2
C removing "Microsoft "End-User License Agreement" window BCM (Business Contact Manager) 1
S Removing "Research" menu item Outlook VBA and Custom Forms 1
H removing attachments... Outlook VBA and Custom Forms 1
P Forwarding emails issue with special characters replacing text body Using Outlook 1
Marc2019 Cannot input Vietnamese Characters on my MSOutlook 2016 in Windows 7 Using Outlook 0
B Instant search with special characters Using Outlook 1
wallisellener "The database name cannot exceed 50 characters" BCM (Business Contact Manager) 2
M Outlook adds strange characters inserted into the Subject Line Using Outlook 2
I Random Chinese characters in email copied from a pst file. Exchange Server 2016 Public Folder Exchange Server Administration 1
H In Exchange 2010, how to block an email containing an attachment that has foreign characters Exchange Server Administration 1
Jon Flemibng Funny characters when printng email Using Outlook 3
J Swedish characters in Outlook folder names Using Outlook 0
B Line/Body width one or two characters Using Outlook 1
M Jolly characters in Outlook 2007 searching Using Outlook 3
L Different characters transfered from Yahoo to Outlook Using Outlook 0
C extra characters added to email when exporting Using Outlook 2
B Outlook contacts with Hotmail. - Error >>>The text exceeds the limit of 1024 characters. Type a shor Using Outlook 4
W Recipients of Outlook messages have characters/words missing in body of message Using Outlook 6
T Get rid of having to type verification characters to send mail BCM (Business Contact Manager) 2
S Outlook 2007, UTF-8 & national characters Using Outlook 5
H Odd symbol when stripping characters using vba Outlook VBA and Custom Forms 3

Similar threads

Top