如何使用 Excel VBA 另存为 HTML 电子邮件?
How to SaveAs to an HTML email using Excel VBA?
我正在尝试使用 Excel VBA 打开电子邮件模板,进行一些更改并将 (.msg) 保存到文件夹。
一切正常,除了电子邮件有时会保存为“纯文本”正文格式,而不是“HTML”格式。
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Set oOutlookApp = GetObject(, "Outlook.Application")
Set oItem = oOutlookApp.CreateItemFromTemplate(TheTemplateLocation)
'''''Some actions here
oItem.SaveAs SaveToLocation, olMSGUnicode
oItem.Close olDiscard
Set oItem = Nothing
我尝试将 olMSGUnicode
更改为 olMSG
甚至删除它,但没有任何改变。
我也试过oItem.Save
但是邮件自动保存到“草稿”文件夹,这对我没用。
然而,这个问题并不总是发生,有时它被保存为正确的“HTML”版本。但我真的很想确保这个问题不会再发生了。
如果您在 Outlook 之外使用后期绑定代码,例如在 Excel 中,则
常量 olMSGUnicode
和 olDiscard
在 Excel 中不存在,因为它们是 Outlook 常量。您可以使用 Option Explicit
.
轻松看到这一点
因此,您需要先在 Excel 中定义它们才能使用它们,或者改用它们的数值。因此看看 OlSaveAsType enumeration and the OlInspectorClose enumeration.
您可能还想改用 olHTML
。
所以像这样声明它们:
Option Explicit
Public Const olDiscard As Long = 1 ' Changes to the document are discarded.
Public Const olPromptForSave As Long = 2 ' User is prompted to save documents.
Public Const olSave As Long = 0 ' Documents are saved.
Public Const olDoc As Long = 4 ' Microsoft Office Word format (.doc)
Public Const olHTML As Long = 5 ' HTML format (.html)
Public Const olICal As Long = 8 ' iCal format (.ics)
Public Const olMHTML As Long = 10 ' MIME HTML format (.mht)
Public Const olMSG As Long = 3 ' Outlook message format (.msg)
Public Const olMSGUnicode As Long = 9 ' Outlook Unicode message format (.msg)
Public Const olRTF As Long = 1 ' Rich Text format (.rtf)
Public Const olTemplate As Long = 2 ' Microsoft Outlook template (.oft)
Public Const olTXT As Long = 0 ' Text format (.txt)
Public Const olVCal As Long = 7 ' VCal format (.vcs)
Public Const olVCard As Long = 6 ' VCard format (.vcf)
Public Sub Example()
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Set oOutlookApp = GetObject(, "Outlook.Application")
Set oItem = oOutlookApp.CreateItemFromTemplate(TheTemplateLocation)
'''''Some actions here
oItem.SaveAs SaveToLocation, olMSGUnicode
oItem.Close olDiscard
Set oItem = Nothing
End Sub
或者直接使用它们的值
oItem.SaveAs SaveToLocation, 9
oItem.Close 1
我正在尝试使用 Excel VBA 打开电子邮件模板,进行一些更改并将 (.msg) 保存到文件夹。
一切正常,除了电子邮件有时会保存为“纯文本”正文格式,而不是“HTML”格式。
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Set oOutlookApp = GetObject(, "Outlook.Application")
Set oItem = oOutlookApp.CreateItemFromTemplate(TheTemplateLocation)
'''''Some actions here
oItem.SaveAs SaveToLocation, olMSGUnicode
oItem.Close olDiscard
Set oItem = Nothing
我尝试将 olMSGUnicode
更改为 olMSG
甚至删除它,但没有任何改变。
我也试过oItem.Save
但是邮件自动保存到“草稿”文件夹,这对我没用。
然而,这个问题并不总是发生,有时它被保存为正确的“HTML”版本。但我真的很想确保这个问题不会再发生了。
如果您在 Outlook 之外使用后期绑定代码,例如在 Excel 中,则
常量 olMSGUnicode
和 olDiscard
在 Excel 中不存在,因为它们是 Outlook 常量。您可以使用 Option Explicit
.
因此,您需要先在 Excel 中定义它们才能使用它们,或者改用它们的数值。因此看看 OlSaveAsType enumeration and the OlInspectorClose enumeration.
您可能还想改用 olHTML
。
所以像这样声明它们:
Option Explicit
Public Const olDiscard As Long = 1 ' Changes to the document are discarded.
Public Const olPromptForSave As Long = 2 ' User is prompted to save documents.
Public Const olSave As Long = 0 ' Documents are saved.
Public Const olDoc As Long = 4 ' Microsoft Office Word format (.doc)
Public Const olHTML As Long = 5 ' HTML format (.html)
Public Const olICal As Long = 8 ' iCal format (.ics)
Public Const olMHTML As Long = 10 ' MIME HTML format (.mht)
Public Const olMSG As Long = 3 ' Outlook message format (.msg)
Public Const olMSGUnicode As Long = 9 ' Outlook Unicode message format (.msg)
Public Const olRTF As Long = 1 ' Rich Text format (.rtf)
Public Const olTemplate As Long = 2 ' Microsoft Outlook template (.oft)
Public Const olTXT As Long = 0 ' Text format (.txt)
Public Const olVCal As Long = 7 ' VCal format (.vcs)
Public Const olVCard As Long = 6 ' VCard format (.vcf)
Public Sub Example()
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
Set oOutlookApp = GetObject(, "Outlook.Application")
Set oItem = oOutlookApp.CreateItemFromTemplate(TheTemplateLocation)
'''''Some actions here
oItem.SaveAs SaveToLocation, olMSGUnicode
oItem.Close olDiscard
Set oItem = Nothing
End Sub
或者直接使用它们的值
oItem.SaveAs SaveToLocation, 9
oItem.Close 1