2017-09-07 133 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

相關問題