将字符串变量插入Text Body

时间:2017-09-21 19:38:40

标签: vba outlook

我想让两个字符串变量出现在电子邮件的文本正文中。我知道我们可以定义一个正文,然后将该正文作为一个整体插入。我想进一步,并在文本中插入两个项目,并不显示为VBA,而是作为实际名称。

两个字符串变量是电子邮件的前两个收件人的名字,在本例中,在VBA中以strTO和strTO1命名。当前宏创建一个回复并将其插入问候语。我还想让它们出现在文本正文中。

示例:收件人名称分别是Tom Jones和Severus Snape,strTO和strTO1。我按照文本中的说明插入它们:

strbody = "<H2><Font Face=calibri>Good Day, </H2>" & strTO & _
      "Please review your data below (link) and approve.<br>" & _
      "Pleaes contact strTO1 if you have problems.<br>" & _
      "<A HREF=""http://www.google.com.htm"">Click Here</A>" & _
      "<br><br><B>Thank you</B>"

当我运行宏时,我想要它读取的内容(仅为了这篇文章的目的而加粗):
美好的一天,汤姆 请在下面查看您的数据(超链接)并批准 如果您有问题,请联系西弗勒斯 (超链接说“点击这里”)
谢谢

现在发生了什么(空白仅表示空格):
美好的一天 BLANK
请查看下面的数据(超链接“点击此处”)并批准 如果您有问题,请联系 strTO1 谢谢

我希望我可以使用某种标签来插入strTO和strTO1。

下面是整个宏,其中包含上面的代码:

Sub AutoReply()
    Dim oMail As MailItem
    Dim oReply As MailItem
    Dim GreetTime As String
    Dim strbody As String
    Dim SigString As String
    Dim signature As String
    Dim strGreetNameAll As String
    Dim lastname As String
    Dim strgreetname As String
    Dim R As Long
    Dim text
    Dim strTo As String
    Dim strTo1 As String

    Select Case Application.ActiveWindow.Class
        Case olInspector
            Set oMail = ActiveInspector.CurrentItem
        Case olExplorer
            Set oMail = ActiveExplorer.Selection.Item(1)
    End Select

    strbody = "<H2><Font Face=calibri>Good Day, </H2>" & <strTo> & _
          "Please review your data below and approve.<br>" & _
          "Please contact strTO1 if you have problems.<br>" &
          "<A HREF=""http://www.google.com.htm"">Click Here</A>" & _
          "<br><br><B>Thank you</B>"

    SigString = Environ("appdata") & _
            "\Microsoft\Signatures\90 Days.htm"

    If Dir(SigString) <> "" Then
        strGreetName1 = Left$(oMail.SenderName, InStr(1, oMail.SenderName, " ") - 1)
        lastname = Right(oMail.SenderName, Len(oMail.SenderName) - InStr(1, oMail.SenderName, " "))              

        If Dir(SigString) <> "" Then
            signature = GetBoiler(SigString)
        Else
            signature = ""
        End If

        Set oReply = oMail.ReplyAll   

        Select Case Application.ActiveWindow.Class
        Case olInspector
            Set oMail = ActiveInspector.CurrentItem
        Case olExplorer
            Set oMail = ActiveExplorer.Selection.Item(1)
        End Select

        With oReply    
            For R = 1 To .recipients.Count
                Debug.Print .recipients(R)
                strgreetname = Left$(.recipients(R), InStr(1, .recipients(R), " "))
                strGreetName2 = Left$(.recipients(2), InStr(1, .recipients(R), " "))
                strGreetNameAll = strGreetNameAll & strgreetname
                strGreetNameAll1 = strgreetname
                strTo = Left(strGreetNameAll, InStr(.recipients(R), " "))
                strTo1 = Left(strGreetName2, InStr(1, .recipients(R), " "))
                strTo = Left(.recipients(1), InStr(.recipients(1) & " ", " ") - 1)    

                If .recipients.Count > 1 Then
                    strTo1 = Left(.recipients(2), InStr(.recipients(2) & " ", " ") - 1)
                Else
                    strTo1 = ""
                End If
            Next R

            Debug.Print strGreetNameAll

            strGreetNameAll = Left(strGreetNameAll, Len(strGreetNameAll) - 1)

            Debug.Print strGreetNameAll

            .HTMLBody = "<Font Face=calibri>Dear " & strTo & " and " & strTo1 & ", " & strbody & "<br>" & signature
            .Display
        End With
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

你快到了。以下将起作用:

 strbody = "<H2><Font Face=calibri>Good Day, </H2>" & strTo & _
          "Please review your data below and approve.<br>" & _
          "Please contact " & strTO1 & " if you have problems.<br>" &
          "<A HREF=""http://www.google.com.htm"">Click Here</A>" & _
          "<br><br><B>Thank you</B>"

您需要确保strTostrTo1定义并设置,然后再将其传递给strBody

所以,在 strBody循环之后放置For r = 1 to ...