2014-10-17 90 views
1

我正在使用VBA脚本通过Outlook在Excel 2010中进行邮件发送。一切正常,只有一个例外:.SentOnBehalfofName行不起作用。下面是完整的代码SentOnBehalfOf无法在Excel 2010中工作VBA代码

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 

如果我把这个早在代码中,我得到我的签名线回来,但我失去了代表另一方发送的能力。

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

+0

检查[这里](http://stackoverflow.com/questions/ 15800468/can-use-a-text-string-within-sentonbehalfofname),也许你的字符串是关闭的。 – mrbungle 2014-10-17 19:45:31

+0

您是否检查过单元格C4中的字符串是否正确解析为名称? – 2014-10-17 20:22:28

+0

我做了,它确实如此。我找到了一个解决方案,我将接下来发布。 – 2014-10-18 16:58:59

回答

1

这是我的解决方案。我需要将.SentOnBehalfOfName移动到WITH命令中的第一条语句,然后在此之后立即显示。我用.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