如何将 MS Word 格式的内容插入 Outlook 邮件?
Posted
技术标签:
【中文标题】如何将 MS Word 格式的内容插入 Outlook 邮件?【英文标题】:How to insert MS Word formatted content into Outlook mail? 【发布时间】:2018-11-16 08:35:03 【问题描述】:问题
此代码使用 MS Word 发送邮件。
邮件正文与 Word 内容相同,但邮件正文未格式化。
如何将格式化的 Word 文档内容插入邮件正文?
Sub SendDocumentInMail()
Dim bStarted As Boolean
Dim oOutlookApp As Object
Dim oItem As Object
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
'Set the recipient for the new email
.To = "recipient@mail.com"
'Set the recipient for a copy
.CC = "recipient2@mail.com"
'Set the subject
.Subject = "New subject"
'The content of the document is used as the body for the email
.Body = ActiveDocument.Content
.Send
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
【问题讨论】:
也许正文设置为纯文本? MailItem.BodyFormat Property 邮件已经是 html 格式,但从 Word 到 Outlook 邮件时格式丢失。 this 的可能重复,接受的答案列出了可能性。 您也可以发表回答并接受。 ***.com/help/self-answer @niton 感谢您引起我的注意。当我问的时候,我还不知道答案。后来我根据收到的信息找到了解决方案。 【参考方案1】:解决办法
(2018.11.19 编辑)
几个小时后,我找到了解决方案:
Sub SendMail()
Selection.WholeStory
Selection.Copy
Dim olapp As Object
Dim olemail As Object
Dim olInsp As Object
Dim wddoc As Object
On Error Resume Next
Set olapp = GetObject(, "Outlook.Application")
If Err <> 0 Then Set olapp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olemail = olapp.CreateItem(0)
With olemail
.BodyFormat = 3
.To = "example@example.com"
.Subject = "Test mail"
Set olInsp = .GetInspector
Set wddoc = olInsp.wordeditor
wddoc.Content.Paste
.Display
End With
End Sub
【讨论】:
以上是关于如何将 MS Word 格式的内容插入 Outlook 邮件?的主要内容,如果未能解决你的问题,请参考以下文章
Word已存文档中,将原有内容的字体,字号作相应改变,加标题,插入,替换某符号等word文档中怎么替换步骤