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

Paste clipboard contents using VBA

Slipstick Systems

› Developer › Code Samples › Paste clipboard contents using VBA

Last reviewed on December 4, 2018     73 Comments

I need a macro that adds a specific text, plus the content of my clipboard, at the end of the subject of all messages I have selected. How do I get the content of the clipboard automatically, without first pasting it into an Input Box?

Although Outlook VBA doesn't include a paste from clipboard function directly, you can use the MSForms dataobject to transfer the clipboard contents to a string which is then called from VBA. You could also use use the Word object model to copy the message body to the clipboard.

You can also use Word's 'Keep Source Formatting' to paste formatted text into an Item Body. Code sample is at Paste formatted text using VBA

Add code similar to this to your macro:

 Dim DataObj As MSForms.DataObject
 Set DataObj = New MSForms.DataObject
 DataObj.GetFromClipboard

strPaste = DataObj.GetText(1)

The finished code will look something like the following. Note, you will need to have a reference to the Forms library in Tools, References.
Add the forms library as a reference
If you receive a "User-defined type not defined" you are missing the reference to Microsoft Forms 2.0 Object Library. If its not listed, add C:\Windows\System32\FM20.dll or C:\Windows\FM20.dll as a reference.

Sub AddtoSubject()
 Dim ex As Explorer
 Dim mail As MailItem
 Set ex = Application.ActiveExplorer
 Dim strPaste  As Variant
 
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard

strPaste = DataObj.GetText(1)

If strPaste = False Then Exit Sub
If strPaste = "" Then Exit Sub

 For Each mail In ex.Selection
    mail.Subject = mail.Subject & " my text " & strPaste
    mail.Save
 Next mail
 
Set DataObj  = Nothing
End Sub

Copy to Clipboard

What about going in the other direction: copying text to the clipboard? Use PutInClipboard to capture the text.

Remember, if you receive a "User-defined type not defined" you are missing the reference to Microsoft Forms 2.0 Object Library. If its not listed, add C:\Windows\System32\FM20.dll or C:\Windows\FM20.dll as a reference.

Sub CapturetoClipbaord()
Dim oMail As MailItem
DataObj As MSForms.DataObject

    Set oMail = ActiveExplorer().Selection.Item(1)
    Set DataObj = New MSForms.DataObject
    DataObj.SetText oMail.Body
    DataObj.PutInClipboard
End Sub

Use Word Object Model to Copy (and Paste)

Current versions of Outlook use Word as the email as the email editor and can use the Word object model library to do things not normally supported in Outlook.

This sample copies the body of the selected message to the clipboard. To paste, use
objSel.PasteAndFormat (wdFormatOriginalFormatting)

Don't forget to set a reference to Word's Object model in Tools, References.

Sub CopyMessage()
    Dim objMail As Outlook.MailItem
    Dim objInsp As Inspector
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection

Set objMail = Application.ActiveExplorer.Selection.Item(1)

  If Not objMail Is Nothing Then
        If objMail.Class = olMail Then

            Set objInsp = objMail.GetInspector
            If objInsp.EditorType = olEditorWord Then
                Set objDoc = objInsp.WordEditor
                Set objWord = objDoc.Application
                Set objSel = objWord.Selection
        With objSel
            .WholeStory
            .Copy
       End With
            End If
        End If
    End If
    
    Set objMail = Nothing
End Sub

This sample shows how to paste the copied content into a new message:

    Dim objMail As Outlook.MailItem
    Dim objInsp As Inspector
    Dim objDoc As Word.Document
    Dim objSel As Word.Selection

Set objMail = Application.CreateItem(olMailItem)
With objMail
  .To = "Alias@domain.com"
  .Subject = "This is the subject"
  .Display
End With

Set objInsp = objMail.GetInspector
If objInsp.EditorType = olEditorWord Then
    Set objDoc = objInsp.WordEditor
    Set objWord = objDoc.Application
    Set objSel = objWord.Selection
        
objSel.PasteAndFormat (wdFormatOriginalFormatting)
End If

Set objMail = Nothing
End Sub

Paste format types are below. For more information see WdRecoveryType Enumeration (Word).

NameDescription
wdChartPastes a Microsoft Office Excel chart as an embedded OLE object.
wdChartLinkedPastes an Excel chart and links it to the original Excel spreadsheet.
wdChartPicturePastes an Excel chart as a picture.
wdFormatOriginalFormattingPreserves original formatting of the pasted material.
wdFormatPlainTextPastes as plain, unformatted text.
wdFormatSurroundingFormattingWithEmphasisMatches the formatting of the pasted text to the formatting of surrounding text.
wdListCombineWithExistingListMerges a pasted list with neighboring lists.
wdListContinueNumberingContinues numbering of a pasted list from the list in the document.
wdListDontMergeNot supported.
wdListRestartNumberingRestarts numbering of a pasted list.
wdPasteDefaultNot supported.
wdSingleCellTablePastes a single cell table as a separate table.
wdSingleCellTextPastes a single cell as text.
wdTableAppendTableMerges pasted cells into an existing table by inserting the pasted rows between the selected rows.
wdTableInsertAsRowsInserts a pasted table as rows between two rows in the target table.
wdTableOriginalFormattingPastes an appended table without merging table styles.
wdTableOverwriteCellsPastes table cells and overwrites existing table cells.
wdUseDestinationStylesRecoveryUses the styles that are in use in the destination document.
Paste clipboard contents using VBA was last modified: December 4th, 2018 by Diane Poremsky

Related Posts:

  • Paste Formatted Text Using VBA
  • Change Short Date to Long Date Format in a Message
  • Copy a Contact's Mailing Address
  • Create Task or Appointment and Insert Selected Text

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
73 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

Pierre-Henry Delecourt
May 25, 2020 3:26 am

Hi. Thanks a lot for this useful tip. I however can't fidn the reference to Microsoft Form, nor the FM20.dll library.
I bought the Microsoft Office in September 2019. Is there a new name for this library ?
Many thanks in advance

0
0
Reply
Diane Poremsky
Author
Reply to  Pierre-Henry Delecourt
December 23, 2020 12:02 am

You need to click Browse and paste the path in. C:\Windows\System32\FM20.dll 

1
0
Reply
Said
May 22, 2020 3:19 pm

Hi Diane,

Is there a way to paste a picture from clipboard directly using VBA?

Thanks.

0
0
Reply
Diane Poremsky
Author
Reply to  Said
June 5, 2020 12:43 am

No, not in Outlook. You need to use the word object. (Could probably use Excel's object model too.)

0
0
Reply
Victor
April 1, 2020 7:09 pm

How can you save clipboard content to a folder in Word without using userform even if it is using the windows api

0
0
Reply
Diane Poremsky
Author
Reply to  Victor
April 1, 2020 11:00 pm

As far as I know, you need to use the msforms data object -

0
0
Reply
dfa
October 27, 2017 8:42 am

Thank you for the info

0
0
Reply
Rudy
October 21, 2017 3:33 am

This is so helpful. Thanks Admin and all the member of this page.

0
0
Reply
Lu(ky
August 23, 2017 11:56 am

How can "Sub CopyMessage" be modified to copy text up to the first "From" in an email (copy text from the most recent email reply)?

0
0
Reply
Diane Poremsky
Author
Reply to  Lu(ky
August 23, 2017 12:21 pm

You'd need to find it the select everything above it -
With objSel
.Find.ClearFormatting
With objSel.Find
.Text = "From: "
.Replacement.Text = ""
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.Execute
End With
.MoveUp Unit:=wdLine, Count:=1
.HomeKey Unit:=wdLine, Extend:=wdExtend

.MoveUp Unit:=wdScreen, Count:=1, Extend:=wdExtend
' .WholeStory
.Copy
End With

0
0
Reply
Lu(ky
Reply to  Diane Poremsky
August 23, 2017 12:36 pm

I'm sorry, I'm having trouble implementing this new code into the older code. I appreciate the fast reply and help!

0
0
Reply
Diane Poremsky
Author
Reply to  Lu(ky
October 8, 2017 11:05 pm

I would need to see the code to help.

0
0
Reply
Lu(ky
Reply to  Diane Poremsky
August 24, 2017 10:14 am

Unfortunately the above code does not locate the text "From"... the Sub still copies the entire email body. Any suggestion?

0
0
Reply
Diane Poremsky
Author
Reply to  Lu(ky
October 8, 2017 11:04 pm

correct, it only copies the body. you need to use oMail.sendername or oMail.senderaddress to get the frm information.

0
0
Reply
Lu(ky
Reply to  Diane Poremsky
August 24, 2017 11:57 am

I got it to work! Thank you for your help!!

0
0
Reply
Adam
April 6, 2017 1:08 pm

Hello Diane, I'm have a need to pass the value of a highlighted text within an email body directly to a URL. so basically, I would double-click (select) on a specfic key word, click some button on my ribbon and have that run a macro to take the selected key word and insert it into a pre-defined URL. I already have the URL code working. I'm stuck trying to get the selected keyword (in the email message body) copied and passed in my code. I'm not trying to copy the entire message body. Any ideas? Thank you!

0
0
Reply
Diane Poremsky
Author
Reply to  Adam
May 26, 2017 9:50 am

Try using just .copy, removing .wholestory.
With objSel
.Copy
End With

0
0
Reply
Jason Johnson
March 13, 2017 11:16 am

Hi Diane,
I'm trying to paste a collection of Excel cells I've copied into the clipboard into an Outlook email's .body. I'm trying to paste using destination styles so as to preserve the table grid of the cells I'm copying from my spreadsheet. While I can dump the contents unformatted easily from the clipboard, I can't seem to get anything to work using the Word references for using destination styles. I've tried playing around with the code in this post but no joy. Any help would be greatly appreciated.

0
0
Reply
Diane Poremsky
Author
Reply to  Jason Johnson
March 16, 2017 1:32 pm

You definitely need to use the word code https://www.slipstick.com/developer/code-samples/paste-clipboard-contents-vba/#word
do you get any error messages?

This should work to paste -
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection

objSel.PasteAndFormat (wdUseDestinationStylesRecovery)

End If

Valid Format types are here: https://msdn.microsoft.com/en-us/library/office/ff844915.aspx

0
0
Reply

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

Latest EMO: Vol. 30 Issue 28

Subscribe to Exchange Messaging Outlook






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?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

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

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

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

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

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.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

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

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

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

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 © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

:wpds_smile::wpds_grin::wpds_wink::wpds_mrgreen::wpds_neutral::wpds_twisted::wpds_arrow::wpds_shock::wpds_unamused::wpds_cool::wpds_evil::wpds_oops::wpds_razz::wpds_roll::wpds_cry::wpds_eek::wpds_lol::wpds_mad::wpds_sad::wpds_exclamation::wpds_question::wpds_idea::wpds_hmm::wpds_beg::wpds_whew::wpds_chuckle::wpds_silly::wpds_envy::wpds_shutmouth:
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