在VBA中使用WordEditor时Outlook附件.add无法正常工作

时间:2018-12-04 14:36:32

标签: vba outlook

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

在代码中,问题出在我将WordEditor对象设置为粘贴消息之后,而之前添加的任何附件都丢失了,如果我更改了代码,请在WordEditor处理该消息后添加它,则没有任何附件,尽管没有错误显示。

这是代码的简化版本:

    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之后,在该行之后或之前设置的所有附件都将放置在邮件正文中。

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

1 个答案:

答案 0 :(得分:0)

您可以参考以下代码:

 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