如何将MS Word格式的内容插入Outlook邮件?

时间:2018-11-16 08:35:03

标签: vba outlook ms-word

问题

此代码使用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

1 个答案:

答案 0 :(得分: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