A visitor to OutlookForums, PGilm is using VBA to create a meeting request and wanted to paste formatted text into the body.
While it's fairly straightforward if you are pasting into a mail item, you can't use HTMLBody or olHTMLFormat with non-mail items. You can use Word's 'Keep Source Formatting' paste command, provided you are using Word as the email editor. (It's the only editor in Outlook 2007 and newer.)
You will need to set a reference to the Word Object Model in the VB editor's Tools, References menu.
This sample code creates an appointment form with the clipboard contents pasted in the appointment body.
To remove the formatting, use wdFormatPlainText.
Sub PasteFormattedClipboard() On Error Resume Next Dim olCal: Set olCal = Application.CreateItem(1) olCal.Subject = "Testing " & Now olCal.Location = "Here" olCal.Display Dim objItem As Object Dim objInsp As Outlook.Inspector Dim objWord As Word.Application Dim objDoc As Word.Document Dim objSel As Word.Selection Set objItem = olCal ' Application.ActiveInspector.currentItem Set objInsp = objItem.GetInspector Set objDoc = objInsp.WordEditor Set objWord = objDoc.Application Set objSel = objWord.Selection objSel.PasteAndFormat (wdFormatOriginalFormatting) 'objSel.PasteAndFormat (Word.WdRecoveryType.wdFormatOriginalFormatting) Set objItem = Nothing Set objInsp = Nothing Set objDoc = Nothing Set objWord = Nothing Set objSel = Nothing End Sub
PasteAndFormat members
The following is a list of the PasteAndFormat options available to use. wdFormatOriginalFormatting and wdFormatPlainText are the most frequently used paste formats that would cover most situations, however you can use other paste formats.
Member name | Description |
---|---|
wdSingleCellText | Pastes a single cell as text. |
wdSingleCellTable | Pastes a single cell table as a separate table. |
wdListContinueNumbering | Continues numbering of a pasted list from the list in the document. |
wdListRestartNumbering | Restarts numbering of a pasted list. |
wdTableInsertAsRows | Inserts a pasted table as rows between two rows in the target table. |
wdTableAppendTable | Merges pasted cells into an existing table by inserting the pasted rows between the selected rows. |
wdTableOriginalFormatting | Pastes an appended table without merging table styles. |
wdChartPicture | Pastes an Excel chart as a picture. |
wdChart | Pastes a Microsoft Excel chart as an embedded OLE object. |
wdChartLinked | Pastes an Excel chart and links it to the original Excel spreadsheet. |
wdFormatOriginalFormatting | Preserves original formatting of the pasted material. |
wdFormatSurroundingFormattingWithEmphasis | Matches the formatting of the pasted text to the formatting of surrounding text. |
wdFormatPlainText | Pastes as plain, unformatted text. |
wdTableOverwriteCells | Pastes table cells and overwrites existing table cells. |
wdListCombineWithExistingList | Merges a pasted list with neighboring lists. |
wdUseDestinationStylesRecovery | Uses the styles that are in use in the destination document. |
In case you are pasting a table from Excel into an Outlook email the below has some additional text so it will autofit the table immediately.
Sub PasteFormattedClipboard()
Dim olCal: Set olCal = Application.CreateItem(1)
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Set objItem = olCal ' Application.ActiveInspector.currentItem
Set objInsp = objItem.GetInspector
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
objSel.PasteAndFormat (wdFormatOriginalFormatting)
Dim aTbl As Word.Table
For i = 1 To objSel.Tables.Count()
Set aTbl = objSel.Tables.Item(i)
aTbl.Columns.PreferredWidth = Unchecked
aTbl.Columns.PreferredWidthType = wdPreferredWidthAuto
Next
Set objItem = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objWord = Nothing
Set objSel = Nothing
End Sub
Dear Diane,
Thanks for your comments they are REALLY helpful!!!
Just wondering is it possible to copy email body to clipboard and preserve the formatting? I tried PutInClipboard but it converts anything into plain text.
Thanks!!!
Jimmy
Yes, but you need to use word objects - see https://www.slipstick.com/developer/code-samples/paste-clipboard-contents-vba/#word for the code sample.
Oh!! That's really unfortunate! :( Well, I was searching through the net for the solution to my problem and it seems there's only one widely accepted solution to achieve it. It's a helper function by a guy named Ron De Bruin and it works perfectly! :)
https://www.rondebruin.nl/win/s1/outlook/bmail2.htm
Hi, this is a really helpful article!! Thanks so much for sharing this :)
One question though, is the above code to be used in Outlook VBA or in Excel VBA ?
Outlook. Are you working with a macro in Excel already? It's possible to do with (different) code from excel too.
Yes :( I already have a macro in Excel and after doing some stuff, I have a range of cells which need to be emailed via outlook and I need the data formats to be retained... any suggestion? :)
Just a range and not a whole sheet? A whole sheet could use the "wordmail" feature where you add an email header to excel.
I've been working on some code samples to send a range... then lost them when Outlook crashed before I hit save. :(
I saw Ron's code. It works with Outlook mail messages but not with Meeting invites.
This is probably because meetings cant be HTML. Is there a way to alter Ron's code for meeting invites?
Does it not work at all with invites or is the text ugly?
This line: Set OutMail = OutApp.CreateItem(0) tells it to create email. That needs changed to 1 for meetings.