如何在电子邮件正文中引用超链接到单元格值?

时间:2018-10-03 06:05:42

标签: excel vba email hyperlink

我想超链接一个单元格,并将该单元格的值作为电子邮件正文中的超链接。

因此,在下面的代码中,而不是“ Hello”应引用单元格的值。假设Range(“ A1”)等于100,则电子邮件正文中的超链接应为100。如果我将Range(“ A1”)更改为101,则电子邮件中的超链接应更改为101。

感谢您的帮助!

我的代码:

Sub SendHyperlinkEmail()

    Dim outApp As Object
    Dim OutMail As Object
    Dim strbody As String

        Set outApp = CreateObject("Outlook.Application")
        Set OutMail = outApp.CreateItem(0)

        strbody = "<A HREF='mailto:z@zzz.com'>Hello</A>"


        On Error Resume Next
        With OutMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = Test
            .HTMLBody = strbody
            .Send
        End With
        On Error GoTo 0

        Set OutMail = Nothing
        Set outApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:0)

我认为串联会起作用。

strbody = "<A HREF='mailto:z@zzz.com'>" & range("a1") & "</A>"

“&”在VBA中用于连接文本和变量/范围/等

答案 1 :(得分:0)

实际上,我本人设法得到了一个相当丑陋的解决方案,因此愿意改进:

子SendHyperlinkEmail()

Dim outApp As Object
Dim OutMail As Object
Dim strbody As String

    Set outApp = CreateObject("Outlook.Application")
    Set OutMail = outApp.CreateItem(0)

strbody = "<table>" & "<tr>" & "<A 
HREF='mailto:mailto:z@zzz.com?subject=Enquiry&Body=I would 
like to'>" _
& range("B2") & "&nbsp;" & range("C2") & "</A>" & "</tr>" & 
 _ "<tr>" & "<A HREF='mailto:mailto:z@zzz.com 
  subject=Enquiry&Body=I would like to'>" _
 & range("B3") & "&nbsp;" & range("C3") & "</A>" & "</tr>" & _
    "<tr>" & "<A HREF='mailto:mailto:z@zzz.com?subject=Enquiry&Body=I would like to'>" _
    & range("B4") & "&nbsp;" & range("C4") & "</A>" & "</tr>" & _
    "</table>"On Error Resume Next
    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = Test
        .HTMLBody = strbody
        .Send
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set outApp = Nothing

结束子