繁体   English   中英

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

[英]SentOnBehalfOf not working in Excel 2010 VBA 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

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

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

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

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

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

我不知道为什么语句需要按此顺序排列,但它有效......

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM