【问题标题】:Select the content of Word document and paste it into the body of Outlook with VBA选择 Word 文档的内容并使用 VBA 将其粘贴到 Outlook 的正文中
【发布时间】:2019-02-07 23:12:01
【问题描述】:

我创建了一个 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

如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: excel vba outlook ms-word


    【解决方案1】:

    你几乎明白了,在粘贴之前尝试显示。另请参阅我所做的小改动

    下面的示例我使用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
    

    【讨论】:

    • 非常感谢您的帮助,如果我在发送之前先显示消息会有所帮助,但我需要它直接发送而不显示它。如果你能继续帮助我,那将是一个巨大的帮助和乐趣。否则我必须先显示然后发送,这会导致 Outlook 消息界面显示片刻然后关闭。使用 Correo .Display 'here objWord.Paragraphs(1).Range。 _ PasteAndFormat 类型:=wdFormatOriginalFormatting .Send End With
    猜你喜欢
    • 2016-06-26
    • 2016-02-27
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多