如何将超链接从列插入Outlook电子邮件正文

时间:2019-04-16 20:03:59

标签: excel vba outlook outlook-vba

我有“ AB”列,其中包含一个超链接,我想通过VBA将其包含在电子邮件中。

超链接每行更改。我可以通过文本拉动该列,但是电子邮件未显示超级链接。

如何使它显示为超链接?

Dim OutApp As Object
    Dim OutMail As Object
    Dim strto As String, strcc As String, strbcc As String
    Dim strsub As String, strbody As String

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

    strto = Cells(FormulaCell.Row, "Y").Value
    strcc = ""
    strbcc = ""
    strsub = "MCR FORM"
    strbody = "Hi " & Cells(FormulaCell.Row, "O").Value & vbNewLine & vbNewLine & _
              "You have a open MCR that needs attention. Please Find the attachted MCR Form for material : " & Cells(FormulaCell.Row, "E").Value & _
              vbNewLine & vbNewLine & Cells(FormulaCell.Row, "AB").Value & vbNewLine & vbNewLine & "Thank you!"


    With OutMail
        .To = strto
        .CC = strcc
        .BCC = strbcc
        .Subject = strsub
        .Body = strbody
        'You can add a file to the mail like this
        .Attachments.Add ("P:\Inventory Control\Public\MCR Form Master.xlsm")
        .Display    ' or use .Send
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

vbNewLine & vbNewLine & Cells(FormulaCell.Row, "AB").Value &

我相信上面的代码需要引用HREF链接?

1 个答案:

答案 0 :(得分:0)

使用HTMLBody Property


示例将显示超链接Click Here

.HTMLBody = "<A href=" & Sht.Range("A1") & "> Click Here </A>"

或这会将A1值显示为链接

"<A href=" & Sht.Range("A1") & ">" & Sht.Range("A1") & "</A>" &

完整代码

Option Explicit
Public Sub Example()

    Dim Sht As Excel.Worksheet
    Set Sht = ThisWorkbook.Worksheets("Sheet1")

    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")

    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = ""

        .HTMLBody = "Hi " & vbNewLine & vbNewLine & _
                    "You have a open MCR that needs attention. " & _
                     vbNewLine & vbNewLine & _
                     "<A href=" & Sht.Range("A1") & "> Click Here </A>" & _
                     vbNewLine & vbNewLine & _
                    "Thank you!"

        'You can add a file to the mail like this
        .Display    ' or use .Send
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Option Explicit Statement (Visual Basic)

强制显式声明文件中的所有变量,或允许隐式声明变量。