Home > Back-end >  Show image from local file in Outlook signature
Show image from local file in Outlook signature

Time:07-22

I am trying to send Outlook email from MS Access VBA. I include a signature file.

However, the image in the signature cannot be shown. The signature file is located in %appdata%Microsoft/Signature folder.

How can I make the sending email show the image?

And here is the content of the folder.
enter image description here

Here is the code.

Private Sub btn_ToOutlook_Click()
    
    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
    'On Error Resume Next
    
    'update workflow status to Renewal Table
    'MsgBox frmPrevious.WorkflowName
    
    'see if Outlook is running and if so turn your attention there
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then 'Outlook isn't running
       'So fire it up
       Set oOutlookApp = CreateObject("Outlook.Application")
       bStarted = True
    End If
    
    ' Change only Mysig.htm to the name of your signature
    SigString = Environ("appdata") & _
                "\Microsoft\Signatures\Peter Hon.htm"

    If Dir(SigString) <> "" Then
        Signature = GetBoiler(SigString)
    Else
        Signature = ""
    End If
    
    'Open a new e-mail message
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem 'and add the detail to it
       .To = "[email protected]"  'send to this address
       .CC = "[email protected]"
       .Subject = "[SW Maintenance Renewal] Request for Credit Check"   " "   Format(Date, "mmm yyyy") 'This is the message subject
       .HTMLBody = frmPrevious.EmailContent & vbNewLine & vbNewLine & Signature
       .Display
    End With
    If bStarted Then 'If the macro started Outlook, stop it again.
       oOutlookApp.Quit
    End If
    
    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing

End Sub

Private Sub Form_Load()
    Set frmPrevious = Screen.ActiveForm
    Set webControl = Me.wb.Object
    With webControl
    End With
    'Me.TimerInterval = 300
    Sleep (5000)
    ShowHtml
End Sub


Private Sub ShowHtml()
    Set webControl = Me.wb.Object
    With webControl
        .Document.Write frmPrevious.EmailContent
    End With
End Sub

Function GetBoiler(ByVal sFile As String) As String
'Dick Kusleika
    Dim fso As Object
    Dim ts As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
    GetBoiler = ts.readall
    ts.Close
End Function

And here is the image
enter image description here

CodePudding user response:

Firstly, do not concatenate HTML strings - concatenating two well-formed HTML documents won't produce a valid HTML document. The two must be merged.

By default, images in HTML signatures reference images using a relative (to the Signatures folder) path. When you create HTML body, the image path is left as is. It is your responsibility to retrieve images from the signature, add images as attachments, set PR_ATTACH_CONTENT_ID MAPI property, and modify the image source in the img tag to reference the image by the cid rather than path. Ditto for the signature styles.

If you want to programmatically insert a signature, Redemption (I am its author) exposes RDOSignature object which implements ApplyTo method (it handles the signature image files and merges HTML styles appropriately).

Also keep in mind that Outlook inserts a signature automatically when a message is displayed as long as the message body is not modified prior to the call. Call MailItem.Display first (Outlook will insert a signature at that point), then append your own text - find the position of the "<body" string in HTMLBody property, look for the next ">" (to take care of the body tags with attributes), and insert your HTML after that ">".

CodePudding user response:

We store the images externally, at imgur (free), and call them by their full URL like this:

<p style="text-align: center; margin-top: 10px; margin-bottom: 0px;">
    <img src="https://i.imgur.com/HbdRTBD.gif" alt="Cactus Data ApS" title="Cactus Data ApS" /><br>
</p>
  • Related