首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在VBA中使用WordEditor时,Outlook attachments.add不工作

在VBA中使用WordEditor时,Outlook attachments.add不工作
EN

Stack Overflow用户
提问于 2018-12-04 22:36:32
回答 1查看 912关注 0票数 0

我有一个Excel文件,其中包含一个嵌入的word对象,用于编辑电子邮件,然后发送它。

在代码中,问题出现在我将WordEditor对象设置为粘贴邮件之后,之前添加的任何附件都丢失了,如果我在WordEditor处理邮件后更改了代码并添加了它,虽然没有显示错误,但没有附加任何内容。

以下是代码的简化版本:

代码语言:javascript
运行
复制
    Dim OlApp As Outlook.Application
    Dim Editor As Object
    Dim ObjMail as Outlook.Mailitem
    Dim WdTag As OLEObject
    Dim WdDocTag As Word.Document
    Dim WSmail as Worksheet

    set WSmail = ThisWorkbook.Sheets("Email")

    Set WdTag = WSmail.OLEObjects("WordTags")
    WdTag.Verb xlVerbPrimary
    Set WdDocTag = WdTag.Object
    WdDocTag.Content.Copy

    Set OlApp = CreateObject("Outlook.Application")

    Set ObjMail = OlApp.CreateItem(olMailItem)

    With ObjMail
       .Attachments.Add "C:\Users\Me\Desktop\txt.txt",,1

       'If I check the Attachments Property of ObjMail here at runtime,
       'I can see the information on the attached file. 
       'However, as soon as the code continues, it vanishes.     

       .BodyFormat = olFormatRichText
       Set Editor = .GetInspector.WordEditor
       Editor.Content.Paste
       Application.CutCopyMode = False

      .To = "Someone"
      .Cc = "Someone"
      .Subject = "MySubject"
      .Display
   End with

编辑:

实际上,我发现在设置.BodyFormat = olFormatRichText之后,在该行之后或之前设置的任何附件都将被放置在消息正文中。

现在的问题是,如何在适当的字段中显示附件,而不是在邮件正文中显示?

EN

回答 1

Stack Overflow用户

发布于 2018-12-05 14:19:44

您可以参考以下代码:

代码语言:javascript
运行
复制
 Set oOutlookMail = oOutlook.CreateItem(olMailItem)    'Start a new e-mail
    With oOutlookMail
        .Display    'Had to move this command here to resolve a bug only existent in Access 2016!

        'To Recipient(s)
        Set oOutlookRecip = .Recipients.Add(sTo)
        oOutlookRecip.Type = olTo

        'CC Recipient(s)
        If Not IsMissing(sCC) Then
            Set oOutlookRecip = .Recipients.Add(sCC)
            oOutlookRecip.Type = olCC
        End If

        'BCC Recipient(s)
        If Not IsMissing(sBCC) Then
            Set oOutlookRecip = .Recipients.Add(sBCC)
            oOutlookRecip.Type = olBCC
        End If

        .Subject = sSubject    'Subject
        Set oOutlookInsp = .GetInspector    'Retains the signature if applicable
        .Importance = 1    'Importance Level  0=Low,1=Normal,2=High

        '        .BodyFormat = olFormatHTML
        Set oWordEditor = .GetInspector.WordEditor
        'oWordEditor.Content.Paste    'Overwrite any existing content, ie:signature
        oWordEditor.Application.Selection.Start = 0
        oWordEditor.Application.Selection.Paste

        ' Add attachments to the message.
        If Not IsMissing(AttachmentPath) Then
            If IsArray(AttachmentPath) Then
                For i = LBound(AttachmentPath) To UBound(AttachmentPath)
                    If AttachmentPath(i) <> "" And AttachmentPath(i) <> "False" Then
                        Set oOutlookAttach = .Attachments.Add(AttachmentPath(i))
                    End If
                Next i
            Else
                If AttachmentPath <> "" And AttachmentPath(i) <> "False" Then
                    Set oOutlookAttach = .Attachments.Add(AttachmentPath)
                End If
            End If
        End If

        For Each oOutlookRecip In .Recipients
            If Not oOutlookRecip.Resolve Then
                bProbRecip = True
                'Display msg to user?
            End If
        Next

        If bProbRecip = False And bEdit = False Then  'Send the e-mail
            .Send
        End If
    End With

有关详细信息,请参阅此链接:

MS Access – Use Word Document As Outlook E-mail Body

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53615299

复制
相关文章

相似问题

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