簡體   English   中英

選擇Word文檔的內容並用VBA將其粘貼到Outlook的正文中

[英]Select the content of Word document and paste it into the body of Outlook with VBA

我創建了一個 Word 模板,我需要執行以下操作:

  1. 基於該模板創建一個新文檔
  2. 修改新模板的部分數據並復制其所有內容
  3. 打開 Outlook 並將模板粘貼到郵件正文中
  4. 將消息發送給相應的收件人

注意:基本模板將根據其數據用於多個收件人。 基本上和Word對應選項卡實現的功能幾乎一樣,只是自定義了。 此外,VBA 代碼在 Excel 工作表中,因為有收件人。

這是我擁有的代碼,一切正常,直到您到達應將內容粘貼到 Outlook 郵件正文中的行,因為這不會粘貼內容,實際上粘貼不起作用。

Sub EnviarRespuestas()
    Dim editor, OutApp, Correo As Object
    Dim i, j, celda As Integer
    Dim pag1 As Worksheet
    Set pag1 = ActiveWorkbook.Worksheets("send messages")
    wArch = "path of the template"
    celda = 11

'create Document of template
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = True
    objWord.documents.Add Template:=wArch, NewTemplate:=False, DocumentType:=0

'Modify document with data of Excel
    For k = 6 To 8
        With objWord.Selection.Find
            .Text = Sheet1.Range("A" & k).Text
            .Replacement.Text = Sheet1.Range("C" & k).Text
            .Execute Replace:=2
        End With
    Next k

    objWord.Activate

'Copy content of the template modify
    objWord.Selection.WholeStory
    objWord.Selection.End = objWord.Selection.End - 1
    objWord.Selection.Copy

'validate if exists recipients in sheets of excel
    Do While Not pag1.Range("J" & celda).Value = ""
        Set Correo = OutApp.CreateItem(0)
        With Correo
            .To = pag1.Range("J" & celda).Value
            .Subject = "CURSO: " & pag1.Range("C6").Text

    'try of paste content in body 
            .BodyFormat = olFormatRichText
            Set editor = .GetInspector.WordEditor
            editor.Content.Paste

            .Display

            celda = celda + 1
        End With
    Loop
End Sub

如果有人可以幫助我,我將不勝感激。

你快明白了,在粘貼之前嘗試顯示。 另請參閱我所做的小改動

下面的示例我使用wdFormatOriginalFormatting來保持 word doc 和簽名的格式

    Dim Correo As Object
    Set Correo = OutApp.CreateItem(0)
    Set objWord = Correo.GetInspector.WordEditor

    With Correo
        .To = pag1.Range("J" & celda).Value
        .Subject = "CURSO: " & pag1.Range("C6").Text

        .Display 'here
         objWord.Paragraphs(1).Range. _
                PasteAndFormat Type:=wdFormatOriginalFormatting

    End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM