![](/img/trans.png)
[英]Outlook Email and Signature from Excel VBA - .Body vs .HTMLbody
[英]VBA Outlook HTMLBody Signature
VBA 新手。 嘗試自動化一些工作電子郵件。
需要在生成的 Outlook 電子郵件的末尾添加我的簽名。 我將( .HTMLBody = msg
)更改為( .HTMLBody = msg & .HTMLBody
)。 這讓我的簽名顯示,但我的 msg 文本消失了。 當我最后刪除.HTMLBody
並使用我的原始代碼時,我的文本顯示格式正確但沒有簽名。
Sub Email()
Dim OutApp As Object
Dim OutMail As Object
Dim Path As String
Dim strbody As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
msg = "<p>Hello World,</p><br>"
On Error Resume Next
With OutMail
.Display
.To = ""
.CC = ""
.BCC = ""
.Subject = "Fruits Stock " & Path
.HTMLBody = msg & "Hello World" & .HTMLBody
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
這里的任何幫助都會很棒。
您是否使用 Google 搜索過MailItem.HTMLBody
?
它在這里顯示了一個例子:
Sub CreateHTMLMail()
'Creates a new email item and modifies its properties.
Dim objMail As Outlook.MailItem
'Create email item
Set objMail = Application.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.HTMLBody = _
"<HTML><BODY>Enter the message text here. </BODY></HTML>"
.Display
End With
End Sub
如果您希望您的電子郵件采用 HTMl 格式,則需要設置一個標志:
.BodyFormat = olFormatHTML
查找MailItem.BodyFormat
屬性。
在創建用於發送具有特定簽名的電子郵件的循環時,我遇到了類似的問題 - 在我的情況下,我必須從幾個簽名中選擇一個,因為每封電子郵件可能會有所不同。
我將簽名名稱作為字符串變量Signame
到一個單獨的函數中,用於將簽名添加到電子郵件中。
'Code trimmed down a lot to just relevant bits
Sub Email()
'Create email here
Dim Email as outlook.mailitem
Set Email = Application.Createitem(olMailItem)
Email.Htmlbody = "<p>Hello World</p>" & AddSig("My_Signature")
Email.display
End Sub
Function AddSig(Signame as string)
If Len(Signame) > 0 Then
Dim fso As Object, ts As Object
Set fso = CreateObject("Scripting.FileSystemObject"): Set ts = fso.GetFile(Environ("Appdata") & "\Microsoft\Signatures\" & Signame & ".htm").OpenAsTextStream(1, -2)
Signame = Replace(Signame, " ", "%20")
AddSig = "<br>" & Replace(ts.ReadAll, _
Signame & "_files", _
Replace(Environ("Appdata"), " ", "%20") & "\Microsoft\Signatures\" & Signame & "_files")
End If
End Function
所以只需在我放置“My_Signature”的地方替換您的簽名名稱。 但是,盡管我努力使用replace(Signame, " ", "%20")
- 這與 HTML 有關系,但這不適用於名稱中帶有空格的簽名。 我只是重命名了我的簽名以刪除所有空格,但是如果有人看到這個並嘗試調試並成功調試,我個人將非常感激!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.