2017-09-07 88 views
0

我已在Outlook VBA下创建了一个宏,该回复的发件人名添加到了问候语中,为主体添加了一些文本,并在我想要的字体。带有个人收件人姓名(原始电子邮件的发件人姓名)的Outlook回复

我需要帮助的是让宏拉取发件人的所有名称,为他们分配一个值,然后我可以将其放置在电子邮件正文的其他地方。如果做不到这一点,我会解决所有问题的名字,尽管能够移动名字是最好的选择。

例如:发件人是名1,名称2
目前,该宏将仅拉出名1(给“亲爱的名称1”),但 我想获得到“亲爱的NAME 1和NAME,”至少是。 Best将能够在问候语中使用Name1,然后将Name2放置在文本的正文中。

我相信我已经把这个尽我所能,并且现在请求专家帮忙!谢谢!!

Sub AutoAddGreetingtoReply() 
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 R As Outlook.Recipient 
Dim strGreetName As String 

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




strbody = "<H3><B></B></H3>" & _ 
"<br><br><B></B>" & _ 
      "Please visit this website to view your transactions.<br>" & _ 
      "Let me know if you have problems.<br>" & _ 
      "<A HREF=""http://www.google.com"">Questions</A>" & _ 
      "<br><br>Thank you" 

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

On Error Resume Next 

If Dir(SigString) <> "" Then 
      strGreetName = Left$(oMail.SenderName, InStr(1, oMail.SenderName, " ") - 1) 
End If 

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

Set oReply = oMail.ReplyAll 

With oReply 
    .CC = "" 
    .HTMLBody = "<Font Face=calibri>Dear " & strGreetName & "," & R1 & strbody & "<br>" & Signature 
    .Display 
End With 

End Sub 
+0

On Error Resume Next阻碍了学习。 http://www.cpearson.com/Excel/ErrorHandling.htm – niton

回答

0

给出一个字符串“第一个最后”,然后获取字符串的右侧像这样

sndrName = oMail.SenderName 
lastName = right(sndrName, len(sndrName) - InStr(1, sndrName, " ")) 

使用在你的代码的格式为:

strGreetName = Left$(oMail.SenderName, InStr(1, oMail.SenderName, " ") - 1) 
lastName = right(oMail.SenderName, len(oMail.SenderName) - InStr(1, oMail.SenderName, " ")) 

如果有空间在InStr文本中返回位置。 https://msdn.microsoft.com/en-us/vba/language-reference-vba/articles/instr-function

原始邮件有一个发件人。 ReplyAll具有收件人,包括原始邮件发件人。

Option Explicit 

Private Sub ReplyFirstNames() 

Dim oMail As mailitem 
Dim oReply As mailitem 

Dim strGreetName As String 
Dim strGreetNameAll As String 

Dim i As Long 

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

Set oReply = oMail.ReplyAll 

With oReply 

    Debug.Print "The reply all recipients are:" 

    For i = 1 To .Recipients.count 
     Debug.Print .Recipients(i) 

     ' Given the format First Last 
     strGreetName = Left(.Recipients(i), InStr(1, .Recipients(i), " ") - 1) 
     strGreetNameAll = strGreetNameAll & strGreetName & ", " 
    Next i 

    Debug.Print strGreetNameAll 
    ' remove extra comma and space from end 
    strGreetNameAll = Left(strGreetNameAll, Len(strGreetNameAll) - 2) 
    Debug.Print strGreetNameAll 

    .htmlbody = "<Font Face=calibri>" & strGreetNameAll & .htmlbody 
    .Display 

End With 

End Sub 
+0

嗨,非常感谢,但请原谅我。我不知道如何去做你说的话。我试图添加该代码并将其作为字符串变暗,但得到了对象变量错误。我只是在学习这一切,所以我感谢您可以给我的任何帮助和指导,谢谢! InStr做什么?谢谢! – learningthisstuff

+0

真棒,再次感谢Niton,我得到了它的工作(本身的成就,相信我!)。这样就拉出了这个人的姓氏,但是我想要做的是获取发件人行中第二个人的姓名。所以当我点击回复时,它会拉出名字并插入问候语。所以它是亲爱的Sender1和Sender2。你知道我怎么能识别第二个发件人,然后应用你的代码?我认为这将是像oMail.SenderName2或类似的东西,但我不知道Outlook如何处理指定。再次感谢你! – learningthisstuff

+0

哇,真棒,能够得到所有的名字,你是怎么做到的?是.recipients片还是strgreetnameall片?我试图运行一个存根宏来让这两个部分一起工作,但目前还没有。有什么办法可以让这两个人一起工作,所以它将这些名称添加到我的代码中,以便将它们添加到正文之前的问候语中?你是个天才! – learningthisstuff

相关问题