向宏添加代码以在从 Excel 发送的不同电子邮件中显示不同的超链接

问题描述 投票:0回答:2

如何添加不同的超链接到发送给不同人的 Excel 电子邮件正文?每封电子邮件都有不同的超链接。这是我到目前为止的代码:

Sub Button1_Click()
    Dim rngCell As Range
    Dim Rng As Range
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strBody As String
    Dim EmailSubject As String
    Dim SendToMail As String
    Dim r As Long
    Application.ScreenUpdating = False
    With ActiveSheet
        If .FilterMode Then .ShowAllData
    End With
    Set OutApp = CreateObject("Outlook.Application")
    Set Rng = Range("T5", Cells(Rows.Count, "T").End(xlUp))
    For Each rngCell In Rng
        r = rngCell.Row
       If Range("J" & r).Value = "" And Range("K" & r).Value <> "" And Range("I" & r).Value <= Date Then
            Range("J" & r).Value = Date
            Set OutMail = OutApp.CreateItem(0)
            strBody = "According to my records, your " & Range("A" & r) & Range("S" & r).Value & _
                " contract is due for review. This contract expires " & Range("K" & r).Value & _
                ".  It is important you review this contract ASAP and email me " & _
                "with any changes that are made.  If it is renewed or rolled over, please fill out the " & _
                "Contract Cover Sheet which can be found in the Everyone folder " & _
                "and send me the Contract Cover Sheet along with the new original contract."
            SendToMail = Range("T" & r).Value
            EmailSubject = Range("A" & r).Value
            On Error Resume Next
            With OutMail
                .To = SendToMail
                .CC = "email address removed for privacy reasons"
                .BCC = ""
                .Subject = EmailSubject
                .Body = strBody
            .Display ' You can use .Send
            End With
        End If
    Next rngCell
    Application.ScreenUpdating = True
End Sub
excel vba outlook
2个回答
1
投票

在代码中,您正在处理纯文本消息正文:

.Body = strBody

要设置超链接,您需要为消息正文准备 HTML 标记,然后设置 HTMLBody 属性,该属性返回表示指定项目的 HTML 正文的字符串。


Outlook 对象模型支持三种主要的自定义邮件正文的方式:

  1. Body 属性返回或设置表示 Outlook 项目的明文正文的字符串。
  2. MailItem类的
    HTMLBody
    属性返回或设置表示指定项目的HTML正文的字符串。设置
    HTMLBody
    属性将始终立即更新 Body 属性。例如:
     Sub CreateHTMLMail() 
       'Creates a new e-mail item and modifies its properties. 
       Dim objMail As Outlook.MailItem 
       'Create e-mail item 
       Set objMail = Application.CreateItem(olMailItem) 
       With objMail 
        'Set body format to HTML 
        .BodyFormat = olFormatHTML 
        .HTMLBody = "<HTML><BODY>Enter the message <a href="http://google.com">text</a> here. </BODY></HTML>" 
        .Display 
       End With 
     End Sub
  1. Word对象模型可用于处理消息正文。请参阅第 17 章:使用项目主体了解更多信息。

0
投票

您应该能够添加我猜测 Excel 工作表单元格中已经存在的超链接。如果链接的排列方式使其位于名称行旁边的单元格中,则您可以仅使用当前的 for 循环。只需添加 & Range("在此处输入带有超链接的单元格").value。对于大部分来说应该不是问题。

如果超链接只有几个,并且需要检查发送给谁,则可以在 for 循环中使用 IF 条件。

© www.soinside.com 2019 - 2024. All rights reserved.