SentOnBehalfOf 在 Excel 2010 VBA 代码中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26432256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:54:30  来源:igfitidea点击:

SentOnBehalfOf not working in Excel 2010 VBA Code

excelvbaexcel-vbaoutlook-vbaoutlook-2010

提问by Mike Marshall

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

我正在编写一个 VBA 脚本,用于在 Excel 2010 中通过 Outlook 发送邮件。一切都运行良好,但有一个例外:.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

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

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

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.

我正在寻找一种解决方案,可以让我做到这两点。任何帮助,将不胜感激。

采纳答案by Mike Marshall

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!

这是我的解决方案。我需要将 .SentOnBehalfOfName 移动到 WITH 命令中的第一条语句,然后立即移动 .Display 。我用 .HTMLBody 替换签名行的字符串以拉入签名行。代码现在运行良好!

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