SentOnBehalfOf not working in Excel 2010 VBA Code

╄→гoц情女王★ 提交于 2019-12-02 11:12:29

问题


I am working on a VBA script for mailing through Outlook in Excel 2010. Everything runs fine with one exception: the .SentOnBehalfofName line will not work. Here is the complete code

 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

If I remove this section the .SentOnBehalfOf works, but I lose my signature line:

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

If I put this back in the code, I get my signature line back, but I lose my ability to send on behalf of another party.

I'm looking for a solution that allows me to do both. Any help would be appreciated.


回答1:


Here is my solution. I needed to move the .SentOnBehalfOfName to the first statement in the WITH Command, then .Display immediately after that. I replace the string for signature line with .HTMLBody to pull in the signature line. Code runs fine now!

I don't know why the statements need to be in this order, but it works.......

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


来源:https://stackoverflow.com/questions/26432256/sentonbehalfof-not-working-in-excel-2010-vba-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!