Outlook attachments.add не работает при использовании WordEditor в VBA - PullRequest
0 голосов
/ 04 декабря 2018

У меня есть файл 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 голосов
/ 05 декабря 2018

Вы можете обратиться к приведенному ниже коду:

 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 - использовать документ Word в качестве электронной почты OutlookТело

...