Adding Outlook signature using OLE automation. RRS feed

  • Question

  • Is it possible to add a signature to an Outlook message using OLE automation?


    Outlook is configured to automatically add a signature to outgoing e-mails. If I don't try to set objMailItem.HTMLBody to any value after creating the Mail Item object, the signature is in the e-mail as expected. If the HTMLBody is set to a value, the signature is lost. Trying to pre-pend the application data to the signature that should exist in the body still loses the signature. I'm using:  objMailItem.HTMLBody = applData + objMailItem.HTMLBody;  (This also results in an Outlook warning that a program is trying to access e-mail addresses in Outlook. If using an automatic signature can't be made to work, can OLE automation be used to go through the Insert / Signature process to add it?


    The code in JavaScript is basically:

       objOutlook = new ActiveXObject("Outlook.Application");

       objMailItem = objOutlook.CreateItem(olMailItem);

       var myNamespace = objOutlook.GetNameSpace("MAPI");
       var myFolder = myNamespace.GetDefaultFolder(6);
      objMailItem = objOutlook.CreateItem(olMailItem);   

      objMailItem.HTMLBody = applData + objMailItem.HTMLBody;


    How can I keep the automatic signature from being lost when adding data to the body of the e-mail, or how can I add a signature after the data has been added to the body?


    (As an aside question, why do I need to execute the GetNamespace and GetDefaultFolder methods before using the CreateItem method if Outlook isn't running? I've seen VBA code examples going straight from creating the Outlook.Application object to using the CreateItem method. I wasn't able to get this to work in VBS unless Outlook was running, or I added the GetNameSpace and GetDefaultFolder code.)


    Thank you for your assistance,




    Friday, October 26, 2007 1:59 PM

All replies

  • I have the same problem and did not found a solution yet. Searching MSDN brought up nothing.


    About your second point: I don't use the mehtods GetNamespace and GetDefaulfolder:


    • Create OLE-objects O_Outlook, O_MailItem and O_Attachment
    • O_MailItem = O_Outlook.CreateItem
    • O_MailItem.To = H_Recipient
    • O_MailItem.HTMLBody = H_Text
    • O_MailItem.Save


    Wednesday, November 7, 2007 3:39 PM
  • The only way I've been able to get it to work to date is to read the Outlook signature file in the \Documents and Settings\<user_name>\Application Data\Microsoft\Signatures directory and append it to the message. The ActiveX WScript.shell gave me access to a USERNAME environment variable for the file path and to find a signature file of the same name.

          shell = new ActiveXObject("WScript.shell"); 
          user_name = shell.ExpandEnvironmentStrings("%USERNAME%");  

    The ActiveX FileSystemObject allows me to read the signature file from JavaScript. The file extension is set based on the value of objMailItem.BodyFormat. A BodyFormat of 1 indicate to use a '.txt' extension, 2 to use a '.htm' extension, and 3 to use a '.rtf' extension. An Outlook signature creates all three of these files. Embedded graphics are added to a sub-directory. (Didn't try to get graphics working.)


             fso =  new ActiveXObject("Scripting.FileSystemObject");

              signatureTextStream = fso.OpenTextFile("C:\\Documents and Settings\\" + user_name + "\\Application Data\\Microsoft\\Signatures\\" + user_name + "." + file_ext, 1);
              signatureContents = signatureTextStream.ReadAll();

    Thursday, November 8, 2007 8:35 PM
  • It should be noted that the previous message code only seems to work if the Outlook BodyFormat is set to HTML. For some reason adding the text or RTF signature file to the Body isn't working for me. If you set HTMLBody using the text (BodyFormat is plain text) or RTF (BodyFormat is rtf) signature file, the BodyFormat is overridden to HTML. If you set the 'Body' property using the text or RTF signature file, the BodyFormat remains unchanged, but all of the RTF codes from the RTF signature file are displayed, and the text signature file doesn't display correctly when the BodyFormat is plain text. Maybe something else in Outlook needs to be set to handle the signature file content correctly?

    Friday, November 9, 2007 4:01 PM

    It works for me!


    I did have the Body of the message set to HTML, of course. Just one little detail (actually two):


    • "...\\Signatures\\" + user_name + "." + file_ext, 1); should be "...\\Signatures\\Signature." + file_ext, 1);
    • And this line depends on the language of the OS. In my case "Signatures" and "Signature" had to be translated to Dutch.

    I had to translate the whole thing to CallCOM-instructions and ended up with three lines of Magic-code.


    Thanx for your help, John!




    Friday, November 9, 2007 6:27 PM
  • Opening the text signature file with the system default (-2) in the call to fso.OpenTextFile allows the text signature file to be read correctly. It may then be written to the .Body to be included with a plain text e-mail message.


    The RTF signature file still displays all of the codes when written to the .Body object and the mail format is RTF.


    Tuesday, November 13, 2007 2:58 PM