SentOnBehalfOf在Excel 2010 VBA代码中不起作用 [英] SentOnBehalfOf not working in Excel 2010 VBA Code

查看:243
本文介绍了SentOnBehalfOf在Excel 2010 VBA代码中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VBA脚本通过Excel 2010中的Outlook进行邮件发送.除了一个例外,一切运行正常:.SentOnBehalfofName行将不起作用.这是完整的代码

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

如果我删除此部分,则.SentOnBehalfOf可以工作,但是我会丢失签名行:

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.

推荐答案

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

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

这篇关于SentOnBehalfOf在Excel 2010 VBA代码中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆