2014-10-17 59 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