'Write before paste table in Outlook - Excel VBA
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
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:\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
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.
I'm using these libraries: Microsoft Office 16.0 Object Library Microsoft Outlook 16.0 Object Library *This is needed Microsoft Word 16.0 Object Library *This is needed
Solution 1:[1]
After a paste, to add above an existing .HTMLBody:
.HTMLBody = "text" & .HTMLBody
Solution 2:[2]
You're already using .Display so no need for second one, keep the following
'Get its Word editor outMail.Display Dim wordDoc As Word.Document Set wordDoc = outMail.GetInspector.WordEditor
After you add the HTMLBody use InsertParagraphAfter Method Example
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
To add something after see example
wordDoc.Range.InsertParagraphAfter wordDoc.Paragraphs(2).Range.PasteAndFormat wdChartPicture wordDoc.Range.InsertParagraphAfter wordDoc.Range.InsertAfter "bla bla"
Solution 3:[3]
Use worddoc.paragraph(3) in the line after "bla bla".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | niton |
| Solution 2 | |
| Solution 3 | Vikram Shankar Mathur |
