【问题标题】:Insert string variable Into Text Body将字符串变量插入文本正文
【发布时间】:2017-09-22 18:57:22
【问题描述】:

我想让两个字符串变量出现在电子邮件的正文中。我知道我们可以定义一个正文,然后将该正文作为一个整体插入。我想更进一步,在文本中插入两个项目,并且不显示为 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>"

我希望它在运行宏时读取的内容(粗体仅用于本文的目的):
美好的一天,汤姆
请在下方查看您的数据(超链接)并批准。
如果您有问题,请联系 Severus
(显示“单击此处”的超链接)
谢谢

现在发生的事情(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

【问题讨论】:

  • 关于这个网站的一件事我不明白......每当我发布赞美你们所有人并赞扬所有人的东西时,人们想要编辑它吗?如果我这样做会很糟糕吗?你们都知道这么多,帮助这么多人学习,你应该为此受到表扬!如果不合适,我不会这样做。我只是想表达我的感激之情。

标签: vba outlook


【解决方案1】:

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

 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 ... 循环之后。

【讨论】:

  • @learningthisstuff 你的问题是你想要,"Good Day, Tom" 中的名称之前,但我认为从语法上讲它应该放在之后 - 即Good Day Tom, (并且可能使用day 而不是Day)。如果是这样,请稍微修改此答案,而不是 ...calibri&gt;Good Day, &lt;/H2&gt;" &amp; strTo &amp; _ 使用 ...calibri&gt;Good Day " &amp; strTo &amp; ",&lt;/H2&gt;" &amp; _。这也将确保整个"Good Day Tom" 被视为H2
  • @scottholtzman,哇,我不敢相信它一直在我面前这么简单!现在我明白为什么人们一直告诉我不仅要包含我正在查看的代码,还要包含整个内容!这就是向您展示我的代码只需要向下移动的原因!效果很好,非常感谢!
猜你喜欢
  • 2015-02-04
  • 1970-01-01
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 2013-01-15
  • 2015-10-26
  • 2021-04-16
相关资源
最近更新 更多