Search code examples
excelvbaoutlookhref

Clickable image in email


I have a picture and text. How do I make it so that the person who receives the email can click on the image so that it sends them to a webpage?

    Dim OutApp As Object
    Dim OutMail As Object
        
    Set OutApp = CreateObject("Outlook.Application")
    'OutApp.Session.logon
    Set OutMail = OutApp.CreateItem(0)
        
    On Error Resume Next
        
    Fname = ThisWorkbook.Path & "\Logo.jpg"
        
    Dim cid As String

    With OutMail
        .to = companymail
        .Subject = "Email Title"
                           
        .Body = strBody
        .HTMLBody = "<html><p>First Text</p>" & _
          "<img src=""cid:Logo.jpg""height=80 width=400>" & "<html><p>Second Text </p>"
        .Attachments.Add Fname, 1, 0
        .Attachments.Add fileaddress
        .Display
    End With
                        
    On Error GoTo 0
        
    Set OutMail = Nothing
    Set OutApp = Nothing
End If

Solution

  • You need to wrap the <img> tag with <a> tag:

    <a href="URL/target.html">
    <img src="src=""cid:Logo.jpg"" height=80 width=400 alt="an image" title="The title of this image" style="border-style:none"/>
    </a>
    

    Also, remember that you need to set up a well formed HTML markup for the HTMLBody property. That means you can't specify anything after closing <html> tag. You must keep the structure.