Search code examples
vb.netpostoutlookadd-in

vba outlook embed image with variables


Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
    Dim mailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)
    If Not (mailItem Is Nothing) Then
        If mailItem.EntryID Is Nothing Then
            mailItem.Subject = "Test"
            mailItem.HTMLBody = mailItem.HTMLBody + "<html><img src='http://example.com/pixel.php></html>"
        End If
    End If
End Sub

The above works fine. But when i try and add some variables to the image i get the following error

Error! Filename not specified.

Here is my code when trying to add a variable:

Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
    Dim mailItem As Outlook.MailItem = TryCast(Inspector.CurrentItem, Outlook.MailItem)
    If Not (mailItem Is Nothing) Then
        If mailItem.EntryID Is Nothing Then
            mailItem.Subject = "Test"
            mailItem.HTMLBody = mailItem.HTMLBody + "<html><img src='http://example.com/pixel.php?to=" + mailItem.To + "></html>"
        End If
    End If
End Sub

Any ideas why this happens?


Solution

  • Close your single quote (how did this work without it?)

    "<html><img src='http://example.com/pixel.php></html>"
                    ^
    

    So the other HTML becomes

    "<html><img src='http://example.com/pixel.php?to=" + mailItem.To + "'></html>"
                                                                        ^