本文介绍了在 Outlook 中粘贴表格之前写入 - Excel VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the following code to paste a table in a new email on 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

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

But I need to write a message before the pasted table.I already tried to use the parameters:

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 参数.

But when paste the table it overwrites the .HTMLBody parameter.

请看我正在使用的整个代码:

Bellow the entire code that I'm using:

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:	est.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.

Look that the wordDoc.Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False is after the parameter .HTMLBody. That makes the pasting code overwrite the parameter .HTMLBody.

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

I'm using these libraries:Microsoft Office 16.0 Object LibraryMicrosoft Outlook 16.0 Object Library *This is neededMicrosoft Word 16.0 Object Library *This is needed

推荐答案

粘贴后,添加到现有 .HTMLBody 之上:

After a paste, to add above an existing .HTMLBody:

.HTMLBody = "text" & .HTMLBody

这篇关于在 Outlook 中粘贴表格之前写入 - Excel VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:54