2016-02-17 56 views
2

我創建自定義回覆時,帳戶發送的Outlook發生更改,並且某些字段會自動填充。Outlook中收件人地址字段

首先我會解釋它是如何工作的,我們會收到一封電子郵件。收到此電子郵件的電子郵件地址位於CC中。我在頂部欄中創建了一個按鈕。當我按下此按鈕時,將打開一個新的電子郵件屏幕,其中已填入一些信息,剩下的信息將用於填寫我們自己的信息。

對於最我有一切設置。但有一件事我無法去工作。我希望新的電子郵件發送到原始的TO(收件人)地址。

現在我有這樣的代碼:

Sub ReplyUsingAccount() 
Dim oAccount As Outlook.Account 
Dim objItem As Outlook.MailItem 
Dim oMail As Outlook.MailItem 
Dim strAcc As String 
Dim i As Long 
Set objItem = ActiveExplorer.Selection.Item(1) 
strAcc = "[email protected]" 
For Each oAccount In Application.Session.Accounts 
    If oAccount.DisplayName = strAcc Then 
     Set oMail = Application.CreateItem(olMailItem) 
     With oMail 
      .SendUsingAccount = oAccount 
      .To = objItem.RecipientEmailAddress 
      .Subject = "Aangaande uw bestelling bij " 
         .HTMLBody = "<br><br><br>" & _ 
         "<hr width=""50%"" size=""2"" noshade />" & _ 
         "<font color=""#6699ff"">" & _ 
         objItem.HTMLBody & "</font>" 
      .Display 
     End With 
    End If 
Next oAccount 
Set oAccount = Nothing 
Set objItem = Nothing 
Set oMail = Nothing 
End Sub 

.To = objItem.RecipientEmailAddress將無法​​正常工作。 任何人都有這個解決方案。

在此先感謝。

+0

https://stackoverflow.com/tour – 0m3r

回答

0

我想象你正在使用的內部電子郵件地址返回.SenderEmailAddress屬性它不返回(這意味着它將是EX類型的地址,而不是SMTP)的SMTP的e-mail地址。

下面將返回內部和外部的SMTP地址。

Dim oOutlook As Outlook.Application 
Dim senderAddress As String, recipEntryId As String, SmtpMailAddress As String 
Dim oAddressEntry As Outlook.AddressEntry, oExchangeUser As Outlook.ExchangeUser 
Dim oReply As Outlook.MailItem, oRecipient As Outlook.Recipient 
Dim objItem As Outlook.MailItem 

If objItem.SenderEmailType = "SMTP" Then 

    senderAddress = objItem.SenderEmailAddress 

Else 

    Set oReply = objItem.Reply() 
    Set oRecipient = oReply.Recipients.Item(1) 

    recipEntryId = oRecipient.EntryID 

    oReply.Close OlInspectorClose.olDiscard 

    recipEntryId = oRecipient.EntryID 

    Set oAddressEntry = oOutlook.GetAddressEntryFromID(recipEntryId) 
    Set oExchangeUser = oAddressEntry.GetExchangeUser() 

    senderAddress = oExchangeUser.PrimarySmtpAddress() 

End If 

SmtpMailAddress = senderAddress 

然後,您可以使用getSmtpMailAddress變量作爲.To的e-mail地址。

如果您使用的是Outlook 2010或更高版本,則可以使用.PropertyAccessor Property。我從來沒有使用過這個,但它可能是值得研究的。

0

我不認爲RecipientEmailAddress在VBA中有效。

嘗試SenderEmailAddress.

0

要將所有收件人地址.To領域的一個例子是。

Option Explicit 
Sub Example() 
    Dim olItem As Outlook.MailItem 
    Dim olReply As MailItem ' Reply 
    Dim Recipient As Outlook.Recipient 
    Dim olRecip As String 

    If Application.ActiveExplorer.Selection.Count = 0 Then 
     MsgBox "No Item was Selected " 
     Exit Sub 
    End If 

    For Each olItem In Application.ActiveExplorer.Selection 
     Set olReply = olItem.Reply 

     For Each Recipient In olItem.Recipients 
      olRecip = Recipient.address & ";" & olRecip 
     Next Recipient 

     With olReply 
      .To = olRecip ' all the Recipient 
      .Subject = "Aangaande uw bestelling bij " 
         .HTMLBody = "<br><br><br>" & _ 
         "<hr width=""50%"" size=""2"" noshade />" & _ 
         "<font color=""#6699ff"">" & _ 
         olReply.HTMLBody & "</font>" 
      .Display 
     End With 

    Next 

End Sub 

要添加發件人地址.To = olRecip & ";" & olItem.SenderEmailAddress

相關問題