簡體   English   中英

粘貼表前寫入 Outlook - Excel VBA

[英]Write before paste table in Outlook - Excel VBA

我正在使用以下代碼將表格粘貼到 Outlook 上的新 email 中:

'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 Object Library Microsoft Outlook 16.0 Object Library *這是必需的 Microsoft Word 16.0 Object Library *這是必需的

粘貼后,在現有的 .HTMLBody 上方添加:

.HTMLBody = "text" & .HTMLBody

您已經在使用.Display所以不需要第二個,請保留以下內容

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

添加HTMLBody使用InsertParagraphAfter 方法示例

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"

在“bla bla”之后的行中使用 worddoc.paragraph(3)。

暫無
暫無

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

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