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
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