SentOnBehalfOf不能在Excel 2010 VBA代码中工作

我正在使用Excel 2010中的Outlook通过VBA脚本工作。一切运行良好,但有一个例外:.SentOnBehalfofName行将不起作用。 这是完整的代码

Sub Mail() ' Working in Office 2010-2013 Dim OutApp As Outlook.Application Dim OutMail As Outlook.MailItem Dim strbody As String ' This is for the Body of the email Dim signature As String ' This is for the email signature On Error Resume Next 'Set OutMail = Nothing 'Set OutApp = Nothing Dim sh As Worksheet Set sh = Sheets("Mail") strbody = sh.Range("C9").Value Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail ' This inserts the email signature .Display End With signature = OutMail.HTMLBody With OutMail '.Display .To = sh.Range("C5") .CC = sh.Range("C6") .BCC = sh.Range("C7") .Subject = sh.Range("C8").Value .HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & signature .SentOnBehalfOfName = sh.Range("C4").Value .Display End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub 

如果我删除此部分.SentOnBehalfOf的作品,但我失去了我的签名线:

 Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail ' This inserts the email signature .Display End With signature = OutMail.HTMLBody 

如果我把这个代码放回去,我会得到我的签名行,但是我失去了代表另一方发送的能力。

我正在寻找一个解决scheme,允许我做到这一点。 任何帮助,将不胜感激。

这是我的解决scheme。 我需要将.SentOnBehalfOfName移动到WITH命令中的第一条语句,然后在此之后立即显示。 我用.HTMLBodyreplace签名行中的string以拉入签名行。 现在代码运行良好!

我不知道为什么声明需要按照这个顺序,但它的工作原理…….

 Sub Mail() ' Working in Office 2010-2013 Dim OutApp As Outlook.Application Dim OutMail As Outlook.MailItem Dim strbody As String ' This is for the Body of the email On Error Resume Next 'Set OutMail = Nothing 'Set OutApp = Nothing Dim sh As Worksheet Set sh = Sheets("Mail") strbody = sh.Range("C9").Value Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) With OutMail .SentOnBehalfOfName = sh.Range("C4") .Display .To = sh.Range("C5") .CC = sh.Range("C6") .BCC = sh.Range("C7") .Subject = sh.Range("C8").Value .HTMLBody = "<br>" & strbody & fncRangeToHtml(sh.Range("C13").Value, sh.Range("C14").Value) & .HTMLBody End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub