I am trying to remove a string from selected incoming MS Outlook (2016) e-mail.
The string is two sentences in German language. I use the Replace() function. This principally works. (See my full procedure below.)
The two sentences are sometimes separated by CRLF (Carriage Return, Line Feed) characters, and these are not always at the same place. This seems to be the result of these e-mails passing through various devices before they land in my Outlook inbox.
First address the simpler part of the problem
Before addressing the issue of the CRLF appearing on varying places, I want to create a procedure that deals with strings with CRLF at fixed positions.
How the source code of such a string would look:
(Screen shot history: I saved the e-mail as .html on my harddisk, then opened the .html file in Notepad++, to see the CRLF characters.)
The html tags are not that relevant for me. They can remain in the e-mail. (In fact, the formatting tags vary, too, so it is better to not start tackling them at all.) My only concern is to remove the visible part, i.e. the text "Diese E-Mail kommt... vertrauenswürdig halten".
I tried to catch text with line breaks by including the CR LF part as Chr():
strDelete01 = "Diese E-Mail kommt von Personen" & Chr(13) & Chr(10) & "au?erhalb der Stadtverwaltung. Klicken Sie nur auf Links oder Dateianhnge," & Chr(13) & Chr(10) & "wenn Sie die Personenn für vertrauenswürdig halten."
My procedure does not recognize the string, and accordingly does nothing.
My script so far
Public Sub EditBodyCgReplace()
'Declarations
Dim obj As Object
Dim Sel As Outlook.Selection
Dim DoSave As Boolean
Dim NewBody As String
Dim strDelete01 As String
Dim strDelete02 As String
Dim strDelete03 As String
Dim strDelete04 As String
'Fill the variables
strDelete01 = "Diese E-Mail kommt von Personen au?erhalb der Stadtverwaltung. Klicken Sie nur auf Links oder Dateianh?nge, wenn Sie die Personen für vertrauenswürdig halten."
strDelete02 = "################################################################################"
strDelete03 = <hr>
strDelete04 = "Diese E-Mail kommt von Personen" & Chr(13) & Chr(10) & "au?erhalb der Stadtverwaltung. Klicken Sie nur auf Links oder Dateianhnge," & Chr(13) & Chr(10) & "wenn Sie die Personenn für vertrauenswürdig halten."
'Note: I am playing here with various types of strings at once. For example,
'the procedure will also remove <hr> lines and "#####" strings
'Work with it
If TypeOf Application.ActiveWindow Is Outlook.Inspector Then
Set obj = Application.ActiveInspector.CurrentItem
Else
Set Sel = Application.ActiveExplorer.Selection
If Sel.Count Then
Set obj = Sel(1)
DoSave = True
End If
End If
If Not obj Is Nothing Then
NewBody = Replace(obj.HTMLBody, strDelete01, "")
NewBody = Replace(obj.HTMLBody, strDelete02, "")
NewBody = Replace(obj.HTMLBody, strDelete03, "")
NewBody = Replace(obj.HTMLBody, strDelete04, "")
If NewBody <> "" Then
obj.HTMLBody = NewBody
If DoSave Then
obj.Save
End If
End If
End If
End Sub
Question: What can I do to include the CRLF in the search string?
Follow-up question: What can I do to remove such strings with CRLF included in varying places? Is there a way to use regular expressions? Can VBA in Outlook deal with it? - Idea: if regular expressions work, perhaps the entire CRLF issue is not an issue anymore, as the expression would look something like
"Diese E-Mail kommt von * vertrauenswürdig halten."
and thus include anything - including CRLF - in the middle?
Perhaps important
After doing various experiments I am starting to feel that MS Outlook does not use HTML at all in its e-mails?
I observe I can practically not address any html code in the obj.HTMLBody. I can address plain text. I cannot address parts of html such as "<hr ", or at least that is what I believe to be observing. (There was a moment when I could address "<hr>" and thus delete it, but I cannot recreate the conditions where this worked yesterday.)
I can save the e-mails as html files (outside Outlook, somewhere on my harddisk in a separate folder), and in these files I do see the CRLF and other stuff. But perhaps the e-mails, as long as kept in Outlook itself, are stored using some other code?
So what is this code, and how can I address parts of it for deleting?
See Question&Answers more detail:os