Change Signatures event

Add-in Express™ Support Service
That's what is more important than anything else

Change Signatures event
Please help understanding how to replace signature in reply/forward 
Vasilis Prantzos




Posts: 9
Joined: 2020-08-30
Hello,

I am having some trouble with replacing automated signatures when replying and the user clicks "Change Signature" from the ribbon.
Until now we were reading signature from the disk and assigning it to HtmlBody property of the reply MailItem, however this is not enough, since that signature doesn't have a "_MailAutoSig" attribute which to my understanging signifies to Outlook it is a signature, so when the user clicks Change Signature, the selected signature gets added and is not replaced.

We tried doing something like:


var wordDoc = reply.GetInspector.WordEditor as Microsoft.Office.Interop.Word.Document;
    if (!wordDoc.Bookmarks.Exists("_MailAutoSig"))
        wordDoc.Bookmarks.Add("_MailAutoSig");
    wordDoc.Bookmarks["_MailAutoSig"].Range.Text = "some_text";


which does put the text in the HtmlBody, but when the used changes the signature from the ribbon, it still gets added and not replaced.

Do you know how to go about it? The alternative way would have been to have the ChangeSignature event exposed by AddInExpress, but we didn't find any.

Is such an event available and which is it?

Do you have any insight on how to replace a signature if there isn't an event we can override? Can you help with an example?
Posted 26 Mar, 2021 09:10:23 Top
Andrei Smolin


Add-in Express team


Posts: 18007
Joined: 2006-05-11
Hello Vasilis,

That bookmark is hidden; its name starts with a "_"). To find it you should get the Bookmarks collection and call Bookmarks.ShowHidden=true on it; see https://docs.microsoft.com/en-us/office/vba/api/word.bookmarks.showhidden.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 26 Mar, 2021 10:03:54 Top
Vasilis Prantzos




Posts: 9
Joined: 2020-08-30
Hi Andrei,

Thank you for your response! I will try it out!

Kind regards,
Vasily
Posted 26 Mar, 2021 11:04:37 Top
Maja




Posts: 10
Joined: 2021-03-29
Hello Andrei,

thank you for your response. However, I'm not sure how setting the ShowHidden property to true would solve our problem.
We would still have the problem that the signature is getting added rather than replaced when the user changes the signature from the Ribbon.
Basically, to be more elaborate on what we have. We have an original reply that gets created by framework when the user clicks Reply button, however due to some reasons, we cannot use this original reply MailItem (that btw contains all the information about the signature we need and behaves OK), but we create a new MailItem along the way and copy properties of the original reply to this new object.
What works to some extent is something like copy.HtmlBody = orgReply.HtmlBody, but it doesn't work well for images! Because for them to work properly we would need to do something like copy.Attachments = orgReply.Attachments, and Attachments property is a read-only one. There are some suggestions how to do this (by saving them to disk etc.) but nothing really worked for us.
That is why we decided to try the approach of manipulating the wordDoc.Bookmarks["_MailAutoSig"] as stated in one of the comments above, and it did add "some_text" to the body of the message, but still when we try to change the signature from the Ribbon, the signature gets added and we have both the signature and "some_text" in the message body and we are not quite sure why, as everything we found suggests that it should be possible to actually replace the signature this way.
Of course, the code mentioned was done for testing purposes. The final thing would look something like assigning the signature from the disk as value for wordDoc.Bookmarks["_MailAutoSig"]

  • Can you pinpoint us to how it should be done and if what we mentioned should be the right way?
  • As Vasilis stated above, the other way would be to have the ChangeSignature event exposed so we can manipulate the htmlBody property at this moment, but I don't think such event exists. Right?
Posted 29 Mar, 2021 04:41:59 Top
Andrei Smolin


Add-in Express team


Posts: 18007
Joined: 2006-05-11
Hello Maja,

You need to have a variable of the Word.Bookmarks type: Word.Bookmarks bookmarks. You set it using bookmarks = (reply.GetInspector.WordEditor as Microsoft.Office.Interop.Word.Document).Bookmarks. Then you call bookmarks.ShowHidden=true and look for the _MailAutoSig bookmark.

Finally, you need to release all COM objects your code creates:
- the bookmarks variable
- the variable holding the _MailAutoSig bookmark
- the variable holding the Word.Document object
- the variable holding the Outlook.Inspector object

To achieve this, the code line above should be split so that you have the variables I list. Only in this way in is possible to release them.

Maja writes:
still when we try to change the signature from the Ribbon, the signature gets added and we have both the signature and "some_text" in the message body


You can use the approach above to check 1) whether the _MailAutoSig bookmark exists and 2) find where exactly your added text is located in relation to the _MailAutoSig bookmark.

Maja writes:
I don't think such event exists. Right?


There's no such event. Should there exist a Ribbon button responsible for changing the signature, you would be able to intercept pressing it. But the Outlook UI makes us use a Ribbon item (not a Ribbon button) and you can't intercept choosing a Ribbon item.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 29 Mar, 2021 08:20:41 Top
Maja




Posts: 10
Joined: 2021-03-29
Hey Andrei,

thanks for answering.
Andrei Smolin writes:
You can use the approach above to check 1) whether the _MailAutoSig bookmark exists and 2) find where exactly your added text is located in relation to the _MailAutoSig bookmark.


About this, after better inspecting the HtmlBODY property of the MailItem, I've noticed that the above mentioned code adds the text outside the bookmark scope, so it does something like:

<a name="_MailAutoSig"></a>some_text


where I would expect something like
<a name="_MailAutoSig">some_text</a>


To my understading, adding to the range of the bookmark (be it a text or inserting a file), should add insidethe scope of the bookmark. Do you maybe see what am I missing?
Posted 06 Apr, 2021 03:54:16 Top
Andrei Smolin


Add-in Express team


Posts: 18007
Joined: 2006-05-11
Hello Maja,

Bookmarks are part of the Word document representing the email body; they mat be reflected in the HTMLBody but they live in the Word.Document object.

Accordingly, I don't know whether what you see is expected or not. My suggestion was to check the document, not the HTML body.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 06 Apr, 2021 05:53:40 Top
Maja




Posts: 10
Joined: 2021-03-29
Hey Andrei,

aha, ok. I don't think checking the Word document would really help because Outlook switches the signature based on the attribute in the HTMLBody, so if it is not visible there, it won't be able to replace it.
However, thanks for help.

Cheers,
Maja
Posted 12 Apr, 2021 04:16:21 Top
Andrei Smolin


Add-in Express team


Posts: 18007
Joined: 2006-05-11
Hello Maja,

I strongly believe that the HTML body is only a representation of what they have in the document. That is, I strongly believe that the attribute in the HTMLBody is a result of having the bookmark in the document: change the bookmark content and this will be reflected in HTMLBody once you retrieve it.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 12 Apr, 2021 04:31:46 Top
Maja




Posts: 10
Joined: 2021-03-29
Hello Andrei,

ok, thanks. I will try once more.

Cheers,
Maja
Posted 13 Apr, 2021 03:12:49 Top