• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Paste Formatted Text Using VBA

Slipstick Systems

› Developer › Code Samples › Paste Formatted Text Using VBA

Last reviewed on December 4, 2018     10 Comments

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.)
Keep source formatting command

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 nameDescription
wdSingleCellTextPastes a single cell as text.
wdSingleCellTablePastes a single cell table as a separate table.
wdListContinueNumberingContinues numbering of a pasted list from the list in the document.
wdListRestartNumberingRestarts numbering of a pasted list.
wdTableInsertAsRowsInserts a pasted table as rows between two rows in the target table.
wdTableAppendTableMerges pasted cells into an existing table by inserting the pasted rows between the selected rows.
wdTableOriginalFormattingPastes an appended table without merging table styles.
wdChartPicturePastes an Excel chart as a picture.
wdChartPastes a Microsoft Excel chart as an embedded OLE object.
wdChartLinkedPastes an Excel chart and links it to the original Excel spreadsheet.
wdFormatOriginalFormattingPreserves original formatting of the pasted material.
wdFormatSurroundingFormattingWithEmphasisMatches the formatting of the pasted text to the formatting of surrounding text.
wdFormatPlainTextPastes as plain, unformatted text.
wdTableOverwriteCellsPastes table cells and overwrites existing table cells.
wdListCombineWithExistingListMerges a pasted list with neighboring lists.
wdUseDestinationStylesRecoveryUses the styles that are in use in the destination document.

More Information

WdRecoveryType Enumeration

Paste Formatted Text Using VBA was last modified: December 4th, 2018 by Diane Poremsky

Related Posts:

  • Add the forms library as a reference
    Paste clipboard contents using VBA
  • Merge to email using only Outlook
  • Create Task or Appointment and Insert Selected Text
  • Use Word Macro to Apply Formatting to Email

About Diane Poremsky

A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.

Subscribe
Notify of
10 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Richard (@guest_206924)
June 2, 2017 5:00 am
#206924

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

0
0
Reply
Jimmy (@guest_199890)
July 9, 2016 3:06 am
#199890

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

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Jimmy
October 12, 2016 12:20 am
#202232

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.

0
0
Reply
Shashank Darisi (@guest_186746)
October 2, 2014 4:02 am
#186746

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

0
0
Reply
Shashank Darisi (@guest_186257)
September 23, 2014 5:08 am
#186257

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 ?

0
0
Reply
Diane Poremsky (@guest_186260)
Reply to  Shashank Darisi
September 23, 2014 8:07 am
#186260

Outlook. Are you working with a macro in Excel already? It's possible to do with (different) code from excel too.

0
0
Reply
Shashank Darisi (@guest_186262)
Reply to  Diane Poremsky
September 23, 2014 9:44 am
#186262

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? :)

0
0
Reply
Diane Poremsky (@guest_186735)
Reply to  Shashank Darisi
October 2, 2014 1:21 am
#186735

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

0
0
Reply
DAVID (@guest_197793)
Reply to  Diane Poremsky
April 12, 2016 6:21 pm
#197793

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?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  DAVID
April 12, 2016 9:13 pm
#197796

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.

0
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 28 Issue 27

Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Subscribe to Exchange Messaging Outlook






Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • How to Remove the Primary Account from Outlook
  • Adjusting Outlook's Zoom Setting in Email
  • Move an Outlook Personal Folders .pst File
  • Save Sent Items in Shared Mailbox Sent Items folder
  • Remove a password from an Outlook *.pst File
  • Outlook: Web Bugs & Blocked HTML Images
  • This operation has been cancelled due to restrictions
  • Outlook Auto Account Setup: Encrypted Connection not available
  • Create rules that apply to an entire domain
  • Use PowerShell to get a list of Distribution Group members
  • How to Block Foreign Spam
  • Automatically Open New Outlook when Windows boots
  • Block External Content in New Outlook
  • Save Messages in New Outlook
  • Send Individual Messages when Sending Bulk Email
  • Centrally managed signatures in Office 365?
  • Create a rule to delete spam with no sender address
  • Open Outlook Folders using PowerShell or VBScript
  • Cannot add Recipients in To, CC, BCC fields on MacOS
  • Change Appointment Reminder Sounds
Ajax spinner

Newest Code Samples

Delete Old Calendar Events using VBA

Use PowerShell or VBA to get Outlook folder creation date

Rename Outlook Attachments

Format Images in Outlook Email

Set Outlook Online or Offline using VBScript or PowerShell

List snoozed reminders and snooze-times

Search your Contacts using PowerShell

Filter mail when you are not the only recipient

Add Contact Information to a Task

Process Mail that was Auto Forwarded by a Rule

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in Outlook for Windows.

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




Windows 10 Issues

  • iCloud, Outlook 2016, and Windows 10
  • Outlook Links Won’t Open In Windows 10
  • Outlook can’t send mail in Windows 10: error Ox800CCC13
  • Missing Outlook data files after upgrading Windows?

Outlook Top Issues

  • The Windows Store Outlook App
  • The Signature or Stationery and Fonts button doesn’t work
  • Outlook’s New Account Setup Wizard
  • Outlook 2016: No BCM
  • Exchange Account Set-up Missing in Outlook 2016

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Contact Tools

Data Entry and Updating

Duplicate Checkers

Phone Number Updates

Contact Management Tools

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2023 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment