VBA前景从转发的消息变化

时间:2016-04-18 13:54:09

标签: vba outlook

我正在尝试自动转发邮件,但在此过程中,将// Define a list of strings that will store your names var lastNames = new List<string>(); // Iterate through your objects and access the LastName property of each foreach(Name n in db.Names) { lastNames.Add(n.LastName); } 字段更改为我已添加到Outlook中的网络中的另一个电子邮件地址(不是作为帐户,而是作为我的邮箱)自己的帐户,我可以访问它)

From

现在问题在于Dim recip As String Dim allRecips As String Dim olApp As Outlook.Application Dim olNs As Namespace Dim Fldr As MAPIFolder Dim olMail As Variant Dim i As Integer Dim oAccount As Outlook.Account Set olApp = New Outlook.Application Set olNs = olApp.GetNamespace("MAPI") Set Fldr = olNs.Folders("CSR GER").Folders("Inbox").Folders("Folders").Folders("#Confirmations") i = 1 For Each olMail In Fldr.Items If InStr(olMail.Subject, "XXX BKG CONFIRMATION: " & BL) <> 0 Then Set Recipients = olMail.Recipients For i = Recipients.Count To 1 Step -1 recip$ = Recipients.Item(i).Address Debug.Print recip$ If VBA.Left(recip$, 10) = "/o=NOL-APL" Then GoTo Line2 End If If i = 1 Then strDomain = strDomain & recip Else strDomain = strDomain & recip & "; " End If Line2: Next i sentby = strDomain Set myItem = olMail.Forward myItem.SentOnBehalfOfName = "CSR_EST@xxx.com" myItem.Recipients.Add sentby myItem.Display 。运行SentBehalfOfNamemyItem.Display字段实际显示正确 - From但在发送电子邮件后,您可以看到邮箱发送了正在转发的电子邮件 - “CSR GER”。

有人能帮我理解这里出了什么问题吗?

1 个答案:

答案 0 :(得分:0)

我自己想出来,这是有用的,以备将来参考:

要使.SentOnBehalfOfName正常工作,您需要先在转发的电子邮件中将其设置为null,然后创建副本并在那里进行更改。

Dim recip As String

Dim allRecips As String
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Dim oAccount As Outlook.Account

Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.Folders("CSR    GER").Folders("Inbox").Folders("Folders").Folders("#Confirmations")

i = 1

For Each olMail In Fldr.Items

      If InStr(olMail.Subject, "XXX BKG CONFIRMATION: " & BL) <> 0 Then

 Set Recipients = olMail.Recipients
For i = Recipients.Count To 1 Step -1
recip$ = Recipients.Item(i).Address

  If VBA.Left(recip$, 10) = "/o=NOL-APL" Then
       GoTo Line2
  End If

  If i = 1 Then
        strDomain = strDomain & recip
  Else
        strDomain = strDomain & recip & "; "
  End If

   Line2:

Next i
sentby = strDomain

Set myItem = olMail.Forward

 myItem.SentOnBehalfOfName = "" ' <- set value to null

myItem.Recipients.Add sentby

Set ittm = myItem.Copy ' <- copy the forwarded email  

 With ittm

 .SentOnBehalfOfName = "CSR_EST@xxx.com"

 .Display

 End With

 myItem.Delete '<- remove the forwarded email