learningthisstuff
learningthisstuff

Reputation: 101

Insert string variable Into Text Body

I would like to get two string variables to appear in the text body of an email. I know we can define a body text and then have that body inserted as a whole. I want to take it further and have two items inserted into the text, and appear not as VBA, but as actual names.

The two string variables are the first names of the first two recipients of an email, in this case named in the VBA as strTO and strTO1. The current macro creates a reply and inserts them into the greeting. I would also like to have them appear within the text body.

Example: Recipient names are Tom Jones and Severus Snape, strTO and strTO1, respectively. I insert them as shown here in the text:

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>"

What I want it to read as when I run the macro (bolded just for purposes of this post):
Good Day, Tom.
Please review your data below (hyperlink) and approve.
Please contact Severus if you have problems.
(hyperlink that says "Click Here")
Thank you

What is happening now (Blank means just a blank space):
Good Day BLANK,
Please review your data below (hyperlink that says "Click Here") and approve.
Please contact strTO1 if you have problems.
Thank you

I was hoping I could use tags of some sort to insert the strTO and the strTO1.

Below is the whole macro with the above code rolled in:

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

Upvotes: 0

Views: 2949

Answers (1)

Scott Holtzman
Scott Holtzman

Reputation: 27269

You are almost there. The below will work:

 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>"

You need to make sure strTo and strTo1 are defined and set before you pass them to strBody

So, place strBody after the For r = 1 to ... loop.

Upvotes: 2

Related Questions