在Outlook中粘贴表之前写入 - Excel VBA

时间:2018-03-27 13:24:52

标签: excel vba excel-vba outlook outlook-vba

我使用以下代码将表粘贴到Outlook上的新电子邮件中:

'Copy range of interest
Dim r As Range
Set r = Range("B2:D5")
r.Copy

'Open a new mail item
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim outMail As Outlook.MailItem
Set outMail = outlookApp.CreateItem(olMailItem)

'Get its Word editor
outMail.Display
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor

'To paste as picture
wordDoc.Range.PasteAndFormat wdChartPicture

'To paste as a table
'wordDoc.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

但是我需要在粘贴的表之前写一条消息。 我已经尝试使用参数:

With OutMail
    .To = ThisWorkbook.Sheets("Sheet2").Range("C1").Value
    .CC = ""
    .BCC = ""
    .Subject = "This is the Subject line"
    .HTMLBody = "This is an e-mail"
    ' In place of the following statement, you can use ".Display" to
    ' display the e-mail message.
    .Display
End With

但是粘贴表格时会覆盖.HTMLBody参数。

将我使用的整个代码包含在内:

Sub btn_Copiar_Clique()

'Range("B2:L44").Select

Dim r As Range
Set r = Range("A2:L44")
r.Copy

'Open a new mail item
Dim outlookApp As Outlook.Application
Set outlookApp = CreateObject("Outlook.Application")
Dim outMail As Outlook.MailItem
Set outMail = outlookApp.CreateItem(olMailItem)

'Get its Word editor
outMail.Display
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor


'To paste as picture
'wordDoc.Range.PasteAndFormat wdChartPicture


   With outMail
            .To = ""
            .CC = ""
            .BCC = ""
            .Subject = "Relatório de Captação de Fundos - Private"
            .HTMLBody = "Boa tarde," & "<br>" & "Segue relatório de captação dos fundos vendidos no private." & "<br>"

            'You can add other files also like this
            '.Attachments.Add ("C:\test.txt")
            .Display   'or use .Display
            '.Send
    End With

'To paste as a table
wordDoc.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

End Sub

看看wordDoc.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False位于参数.HTMLBody之后。这使得粘贴代码覆盖参数.HTMLBody

我使用这些库: Microsoft Office 16.0对象库 Microsoft Outlook 16.0对象库*这是必需的 Microsoft Word 16.0对象库*这是必需的

2 个答案:

答案 0 :(得分:0)

粘贴后,在上面添加一个现有的.HTMLBody:

.HTMLBody = "text" & .HTMLBody

答案 1 :(得分:0)

您已经在使用.Display所以不需要第二个,请保留以下内容

'Get its Word editor
outMail.Display
Dim wordDoc As Word.Document
Set wordDoc = outMail.GetInspector.WordEditor

添加 HTMLBody 后,使用 InsertParagraphAfter Method 示例

With outMail
    .To = ""
    .cc = ""
    .BCC = ""
    .Subject = "Relatório de Captação de Fundos - Private"
    .HTMLBody = "Boa tarde," & "<br>" & "Segue fundos vendidos no private."

     wordDoc.Range.InsertParagraphAfter
     wordDoc.Paragraphs(2).Range.PasteAndFormat wdChartPicture
End With

在看到示例

后添加内容
     wordDoc.Range.InsertParagraphAfter
     wordDoc.Paragraphs(2).Range.PasteAndFormat wdChartPicture
     wordDoc.Range.InsertParagraphAfter
     wordDoc.Range.InsertAfter "bla bla"