htmlexcelvbasharepointoutlook

How to use .HTMLBody to hyperlink a cell value?


I have an application where I have an excel workbook that is used as an approval workflow form. The form sends an email to the next person in the workflow as it progresses. These forms live in a SharePoint library where users check in/check out the forms. I want to include a link to the workbook in the emails to save the users time. Also, the workbook needs to open in the desktop version of Excel because there's macros that can not run in the online version.

I have the email functionality in VBA and I pull the recipient and body message from cells. I am putting the link to the workbook in the body message cell. I use Excel formulas to get the full link of the workbook in a cell.

I can not explain why, but when I click on the link in the email, it will open the workbook, but when I click to check out the Excel, it gives me an error that it can't check it out and is read only. If I copy and paste this exact same text into a browser instead of clicking the hyperlink, everything will work just like I intend.

Additional frustration, appending "?web=1" to the URL works and opens in the web version where it can be opened in the desktop app but I do not like this solution as it is bulky. Trying appending "?web=0" has the same result as before where the file can't be checked out and is read only. Sigh.

I want the users to be able to click a link. Therefore I am trying a different approach and trying to use HTML to create a hyperlink from the cell value containing the URL. I am having difficulty getting the cell value with .HTMLBody as a hyperlink.

.HTMLBody = ActiveWorkbook.Worksheets("my sheet").Range("A1")

This ^ returns just the plaintext without any hyperlinking functionality so the user would need to paste it into a browser.

This is my code to generate the emails and what I am trying for .HTMLBody - Code primarily from https://www.excelmojo.com/vba-send-email-from-excel/

    Dim OutlookApp As Object
    Dim OutlookMail As Object
    Dim strbody As String
    Dim fpath As String
    

    ' Create a new instance of Outlook
    Set OutlookApp = CreateObject("Outlook.Application")

    ' Create a new email
    Set OutlookMail = OutlookApp.CreateItem(0)

    ' Set the properties of the email
    With OutlookMail
        '.BodyFormat = olFormatHTML
        .To = ActiveWorkbook.Worksheets("mysheet").Range("A2")
        .CC = ""
        .BCC = ""
        .Subject = ActiveWorkbook.Worksheets("mysheet").Range("A3")
        .HTMLBody = < a href="ActiveWorkbook.Worksheets("mysheet").Range("A1").Value>Help</a>
        .Display ' Use .Send instead of .Display to send the email without displaying it
    End With

    ' Clean up
    Set OutlookMail = Nothing
    Set OutlookApp = Nothing

Long story short, I am looking for help with the syntax of .HTMLBody to include the link from the cell value. Thank you


Solution

  • Just make sure that you pass the content of the cell with the link correctly:

    With OutlookMail
        '.BodyFormat = olFormatHTML
        .To = ActiveWorkbook.Worksheets("mysheet").Range("A2")
        .CC = ""
        .BCC = ""
        .Subject = ActiveWorkbook.Worksheets("mysheet").Range("A3")
        .HTMLBody = "<a href=" & ActiveWorkbook.Worksheets("mysheet").Range("A1").Value & ">Help</a>"
        .Display ' Use .Send instead of .Display to send the email without displaying it
    End With
    

    By the way, you should really use WITH statement in your code.