HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi Everyone,

I'm trying to develop, what I thought would be, a really simple piece of VBA code to search and replace a string of text in the current, open email. On my first attempt, I discovered that the formatting of the area of the message where I was making the change was being completely destroyed. Research on this suggested that I should be using 'Replace(Application.ActiveInspector.CurrentItem.HTMLBody', instead of' Replace(Application.ActiveInspector.CurrentItem.Body', but I'm not entirely sure if even that is the proper way to approach what I'm trying to do here.

Once I switched to using HTMLBody, I found that I could successfully perform a search and replace, without destroying the formatting. However, when it came to attempting to replace the actual string I want to replace, the string was not being detected within the email by the Replace command. Testing 'Search and Replace' on the string using the standard, manual method in Outlook works fine.

I think the problem here is that I have discovered there is HTML formatting code embedded within the string that I want to replace. When you look at the text I need to replace using the standard email view, it looks like this:

first part of replace string | second part of replace string

However, when you view the email body as HTML code, the relevant extract comprises of the following:

<span style='font-size:9.0pt;color:black'>first part of replace string</span><span style='font-size:9.5pt'> | </span>
<span style='font-size:9.0pt;color:black'>second part of replace string</span>

Can anyone help me to understand how I should code this in VBA, so that the HTML code does not get in the way of the search part of the replace command I need to perform. It's important that I select the entire string, as opposed to part of it, because the string I'm searching to replace needs to be unique with the email body and the only way I can achieve that is if I select the entire string (i.e. part one and part two), with the pipe character "|" in the middle. Any assistance anyone could provide to me in how I can overcome this problem would be very much appreciated.

Kind regards,

Steve
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
so you want to replace "first part of replace string | second part of replace string"

If there are no other uses if first string, second string (or | if you need to replace it too), I would do it in 2 parts.
item.htmlbody = replace(html.body, "first part of string", "new string")
item.htmlbody = replace(html.body, "second part of string", "new string")

Because the HTML uses ' not ", find and replacing the html will be fairly easy - no annoying coding to maintain the ".

If the words are used elsewhere and the only difference is the |, replace "first part of replace string</span><span style='font-size:9.5pt'> |" and " | </span><span style='font-size:9.0pt;color:black'>second part of replace string"

Or, do it in one long string - "first part of replace string</span><span style='font-size:9.5pt'> | </span><span style='font-size:9.0pt;color:black'>second part of replace string"

Using the leading <span style='font-size:9.0pt;color:black'> and ending </span> shouldn't be necessary.

To make it less messy use strings for the find and replace values-
strFind = "first part of replace string</span><span style='font-size:9.5pt'> | </span><span style='font-size:9.0pt;color:black'>second part of replace string"
strReplace = "first new</span><span style='font-size:9.5pt'> | </span><span style='font-size:9.0pt;color:black'>second new string"
item.htmlbody = replace(html.body, strFind, strReplace)
 

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi Diane,

Many thanks for your response. I'm going to give this another go, because although I think I already tried your second suggestion, i.e.:

Or, do it in one long string - "first part of replace string</span><span style='font-size:9.5pt'> | </span><span style='font-size:9.0pt;color:black'>second part of replace string"

...I'm now doubting myself. Much as I would like to break this down into two operations, that is not really going to work for me, for the reason I explained within the closing comments of my original post, i.e.:

It's important that I select the entire string, as opposed to part of it, because the string I'm searching to replace needs to be unique within the email body and the only way I can achieve that is if I select the entire string (i.e. part one and part two), with the pipe character "|" in the middle.

I'll revisit this, this time definitely making sure I include the HTML within the search string, and I'll let you know how I get on. The mere fact that you have not told me I'm doing it all wrong is encouragement in itself! :) Many thanks, once again.

Kind regards,

Steve
 

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi,

Yep, as I thought, for some reason the search/replace does not find the string to replace when the HTML code is included within it. If you or anyone else doesn't have any other alternative approaches that immediately come to mind, I'll have to have a play around and see if I can come with another way of doing it. If I crack it, I'll post the solution back here.

Kind regards,

Steve
 

Steve Tomalin

Member
Outlook version
Outlook 2016 32 bit
Email Account
Outlook.com (as MS Exchange)
Hi again,

Well, I can't explain this, but I have just discovered that if I search for the second part of the string, including the pipe character at the front and including the HTML code, that works! As to why the search/replace doesn't work when I include the whole string and the HTML code, I have no idea, but I'm not going to worry about it. Upon reflection, I think the combination of the pipe character and the second part of the string will be enough to keep that part of the string unique within the body of the email. so that is enough to ultimately get me to where I need to be. So, Diane, you were right after all, splitting it up did actually provide the solution I needed, so many thanks for leading me to re-think through that approach, which has now provided the solution, albeit somewhat unexpectedly and inexplicably!

Kind regards,

Steve
 
Similar threads
Thread starter Title Forum Replies Date
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
I Outlook 2003 shows html code when To: field is empty Using Outlook 7
C In need of VBA code to read / parse HTML - Outlook emails Using Outlook 0
S -> [O2007]: get the message HTML original source code? Outlook VBA and Custom Forms 6
D Outlook 2016 64bit, Cannot Save in 'HTML', format Using Outlook 1
K Outlook adds space to rows in HTML email signatures viewed on mobile Using Outlook 1
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
M HTML email signature in Outlook Using Outlook 3
B Programmatically force html send and insert clipboard contents into body Outlook VBA and Custom Forms 0
S Change VBA script to send HTML email instead of text Outlook VBA and Custom Forms 3
U HTML Task Alternate Home Page View Control Using Outlook 3
D Custom form with html hyperlink Outlook VBA and Custom Forms 7
D Keep Original html body when forwarding an email Outlook VBA and Custom Forms 7
S Emails Configured As HTML But Sent As Plain Text Using Outlook 13
D create an html table in outlook custom form 2010 using vba in MsAccess Outlook VBA and Custom Forms 7
George Simpson "Switch to HTML" in Outlook 2016 notes field Using Outlook 1
H Search and delete part of a link inside HTML body message Outlook VBA and Custom Forms 2
S Outlook 2013 doesn't download html messages Using Outlook 0
A How can I load a HTML template into the WordEditor? Outlook VBA and Custom Forms 3
cabinet guy permanent response in html Using Outlook 1
C Signatures in HTML emails Using Outlook 5
I Outlook 2013 Appointments not in HTML Using Outlook.com accounts in Outlook 2
B Replies never HTML Using Outlook 2
L Outlook 2002: HTML Emails Will Not Print: Please Help Using Outlook 0
Diane Poremsky Outlook crashes when viewing HTML Messages Using Outlook 0
Diane Poremsky Create a New Message using an HTML File or Stationery Using Outlook 0
V incorrect html signature printed Using Outlook 2
Z alter the timeout for how long outlook looks for a content-server on an HTML-email? Using Outlook 5
Diane Poremsky Create a New Message using an HTML File or Stationery Using Outlook 0
Y HTML Emails Using Outlook 1
A Lync meeting in outlook lose HTML formatting Using Outlook 3
C Outllok 2010 (14.0.7015.1000 (32bit) converts html mail to rich text when move Using Outlook 1
T HTML email for internal mesages and Text only for External? Using Outlook 1
M Unable to read HTML Using Outlook 2
V Name of attachment in replies (HTML) - possible?? Using Outlook 12
A BCM 2010 converts html e-mails to plain text BCM (Business Contact Manager) 1
D Saving outlook emails in html and attachments Using Outlook 4
T Forcing HTML for replies in Outlook 2013 Using Outlook 4
M HTML mails apprearing broken in outlook 2013 Using Outlook 5
T Html tags in Message body Using Outlook 1
R OL2010; How to ALWAYS Reply/Forward using HTML? Using Outlook 1
J Outlook is not parsing html email, unreadable and attachment not decoded Using Outlook 1
A Calendar to Web HTML - Multiple Files Using Outlook 1
S HTML Contents of an email Jumbled in Outlook 2010 Client Using Outlook 5
F "MSG" type attachments are stripped away when senand previewed with HTML tags. Using Outlook 3
C Save as HTML for multiple emails Using Outlook 1
G No word wrap in HTML email. Using Outlook 8
M Mail in HTML format can't be sent if url or number is in the body Outlook 2010 Using Outlook 2
O Exchange 2003 displaying HTML emails as having attachments Exchange Server Administration 7
G Keyboarding with HTML Email in OL2010-64bit Using Outlook 0

Similar threads

Top