首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将MS Word格式的内容插入Outlook邮件?

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

Stack Overflow用户
提问于 2018-11-16 08:35:03
回答 1查看 128关注 0票数 0

问题

这段代码用MS Word发送邮件。

邮件正文与单词内容相同,但邮件正文未格式化。

如何将格式化的Word文档内容插入邮件正文?

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-11-20 06:47:19

解决方案

(2018.11.19编辑)

几个小时后,我找到了解决办法:

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53334107

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档