none
Add and remove a Text Message and Signature to Reply, Reply to All or a Forward RRS feed

  • Question

  • Below is code that I am using to add and remove a Text Message and Signature to the body of an email.

    Both code segments work perfectly when used against a new email, however when I try the same code against a Reply, Reply to All or a Forward email, the remove code segment fails.

    The add code adds the text and signature no worries but when I try to use the remove code, it can now locate the added Text Message and Signature. 

    When I looked at the underlining source code of the email, the text and signature that I added is not there, yet in the ui side of the email I can see it.

    I have tried to save the instance of the email at different times to see if that would help but no joy.

    I have also tried re-setting the email format to mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;, yet still no joy.

    So I guess the question is, what am I doing wrong in the adding of the Text Message and Signature that the remove code can not see it?

    Add Text Message and Signature Code:

    Outlook.Application app = new Outlook.Application();
    Outlook.MailItem mail = null;
    mail = (Outlook.MailItem)app.ActiveInspector().CurrentItem;

    mail.HTMLBody = "<InternalOnly><html><body>" + Toggles.strGetBeginTable + Toggles.strGetLine + Toggles.strGetBodyInternalOnlyMessage + Toggles.strGetLineSpacer + Toggles.strGetEndTable + "</body></html></InternalOnly>" + mail.HTMLBody;

    mail.HTMLBody = Toggles.strGetSpacers + "<AddSignature><html><body>" + Toggles.strGetBeginTable + addSignatureByNameLogic(Toggles.strGetDefaultSignature) + Toggles.strGetEndTable + "</body></html></AddSignature>" + mail.HTMLBody;


    Remove Text Message and Signature Code:

    mail.HTMLBody = mail.HTMLBody.Substring(mail.HTMLBody.IndexOf("<" + strPassRemoveValue + ">"), mail.HTMLBody.IndexOf("</" + strPassRemoveValue + ">") - mail.HTMLBody.IndexOf("<" + strPassRemoveValue + ">") + 7);

    Thursday, December 6, 2012 1:59 PM

Answers

  • You cannot concatenate 2 HTML strings and produce a valid HTML string.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 2:15 PM
  • if this is outlook >= 2007, obtain Word Document interface from Inspector.WordEditor and use word API to manipulate contents of mail body.
    Thursday, December 6, 2012 6:48 PM
  • the problem is not that you have 2 strings. The problem is that while each string is a valid HTML string, the resulting string is not. For example:

    string1 = <html><body>test 1</body></html>

    string2 = <html><body>test 2</body></html>

    Concatenating these strings will produce

    <html><body>test 1</body></html><html><body>test 2</body></html>

    (which is invalid), while you really want.

    <html><body>test 1 test 2</body></html>

    You need to merge the strings. As Damian suggested, either use the Word Object Model, or parse the HTML yourself: look for the position of the first occurrence of "<body", then find the next occurrence of ">", then insert your HTML code (without the <html> and <body> tags) at that position.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 8:14 PM
  • 1. try calling save on that mailitem after changing body

    2. if that does not work, read entryid of that mail (it will not be empty after save), release that mail and open it again by calling Session.GetItemById

    Friday, December 7, 2012 2:09 PM

All replies

  • You cannot concatenate 2 HTML strings and produce a valid HTML string.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 2:15 PM
  • even though this works when ran against a new email?
    Thursday, December 6, 2012 2:54 PM
  • Outlook does try to parse invalid HTML, but there is only so much it can do...

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 3:01 PM
  • So, I tried to make one big HTML string (rather than the 2) and I get the same issue.

    the remove part of the code can not see the HTML string

    I even tried working only with one string by itself and still no joy

    • Edited by niyack Thursday, December 6, 2012 3:21 PM
    Thursday, December 6, 2012 3:20 PM
  • given what I am trying to do with the text message and signature, would Outlook Redemption do the job better and/or easier?

    Keep in mind, this does not have to be done in HTML if there is another way. I only did the HTML version because I could not get any other way working

    What say you?

    Thursday, December 6, 2012 3:27 PM
  • if this is outlook >= 2007, obtain Word Document interface from Inspector.WordEditor and use word API to manipulate contents of mail body.
    Thursday, December 6, 2012 6:48 PM
  • ok, does that apply to Office 2010 also?

    I should have mentioned that, Sorry....

    Thursday, December 6, 2012 8:04 PM
  • the problem is not that you have 2 strings. The problem is that while each string is a valid HTML string, the resulting string is not. For example:

    string1 = <html><body>test 1</body></html>

    string2 = <html><body>test 2</body></html>

    Concatenating these strings will produce

    <html><body>test 1</body></html><html><body>test 2</body></html>

    (which is invalid), while you really want.

    <html><body>test 1 test 2</body></html>

    You need to merge the strings. As Damian suggested, either use the Word Object Model, or parse the HTML yourself: look for the position of the first occurrence of "<body", then find the next occurrence of ">", then insert your HTML code (without the <html> and <body> tags) at that position.


    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 8:14 PM
  • as stated, I tried this and it failed
    Thursday, December 6, 2012 8:55 PM
  • "Failed" as in "I could not merge two HTML strings"? Have you tried the algorithm that I outlined above?

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Thursday, December 6, 2012 10:46 PM
  • >= 2007 means that 2010 falls into that category

    Friday, December 7, 2012 8:23 AM
  • Sorry, failed as in after I merged the HTML string then tried to add it to the body of the email as you guys have suggested, it acted the exact same way as it did when I had the 2 different strings.

    It does not matter how I add the HTML/text, for some reason the underlining html code of the email never sees it, therefore the remove code can not find it. if one looks at the email as a user would, one can see the added text and signature, however if one looks at that same email from under the covers via code, the added text and signature are not showing up. What makes it even more weird, is that this all works on a new email but fails on a reply, reply to all and a forward.

    Friday, December 7, 2012 1:13 PM
  • 1. try calling save on that mailitem after changing body

    2. if that does not work, read entryid of that mail (it will not be empty after save), release that mail and open it again by calling Session.GetItemById

    Friday, December 7, 2012 2:09 PM
  • Please post the relevant snippet of your code and show the HTML that your code is setting as well as the HTML that you retrieve back.

    Dmitry Streblechenko (MVP)
    http://www.dimastr.com/redemption
    Redemption - what the Outlook
    Object Model should have been
    Version 5.4 is now available!

    Friday, December 7, 2012 8:43 PM