Search code examples
vbavb.netoutlookoffice-interop

Adding the default signature to Outlook email


I have an application that will fill out the To/Subject/Body of an outlook email:

Dim App As New Outlook.Application
Dim MailItem As Outlook._MailItem = DirectCast(App.CreateItem(Outlook.OlItemType.olMailItem), Outlook._MailItem)


Dim appDataDir As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Microsoft\Signatures"
Dim Signature As String = String.Empty
Dim diInfo As New DirectoryInfo(appDataDir)

If diInfo.Exists Then
    Dim fiSignature As FileInfo() = diInfo.GetFiles("*.htm")
        If fiSignature.Length > 0 Then
            Dim sr As New StreamReader(fiSignature(0).FullName, Encoding.[Default])
            Signature = sr.ReadToEnd()

            If Not String.IsNullOrEmpty(Signature) Then
                Dim fileName As String = fiSignature(0).Name.Replace(fiSignature(0).Extension, String.Empty)
                Signature = Signature.Replace(fileName & Convert.ToString("_files/"), (Convert.ToString(appDataDir & Convert.ToString("/")) & fileName) + "_files/")
            End If
        End If
End If

With MailItem
    .To = "asdf"
    .Subject = "asdf"
    .Body = txtTemplatePreview.Text & vbNewLine
End With
MailItem.Display(True)

So the function of the first If Then statement is to append my default signature to the end of the email. However, when this code is run, the signature that is appended looks to be HTML code instead of the signature itself.

In addition, I'm told that the first If Then statement will fail if the user has more than one signature. Is there a way to circumvent this?


Solution

  • Work with HTMLBody Property

    The property Returns or sets a String representing the HTML body of the specified item. The HTMLBody property should be an HTML syntax string. Read/write.