Please Note: This article is written for users of the following Microsoft Word versions: 2007 and 2010. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Processing Information Pasted from a PDF File.

Processing Information Pasted from a PDF File

by Allen Wyatt
(last updated February 27, 2019)

20

Zach is constantly pasting quotes from PDF files into the body of his Word documents. He'd like to have a macro specifically for pasting from PDF that pastes without any formatting and automatically removes the paragraph breaks that are at the end of each line of the pasted text.

It is relatively easy to work with text in this manner in a macro. All you need to do is move the information from the Clipboard to a string variable. Once it is in the variable, there is no longer any formatting associated with the text and you can search for and replace the paragraph breaks. The following macro performs both steps:

Sub PastePDFClean()
    Dim MyData As DataObject
    Dim sTextIn As String
    Dim x As Integer
    Dim y As Integer

    Set MyData = New DataObject
    MyData.GetFromClipboard
    sTextIn = MyData.GetText

    x = InStr(sTextIn, vbCr)
    y = 1
    While x > 0
        sTextIn = Left(sTextIn, x - 1) & Mid(sTextIn, x + 1)
        y = x + 1
        x = InStr(y, sTextIn, vbCr)
    Wend

    Selection.TypeText sTextIn
    Set MyData = Nothing
End Sub

Remember; the macro works on whatever is in the Clipboard. So, in order to run the macro properly on a PDF selection, you need to copy the selection to the Clipboard and switch to your Word document before you run the macro.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11626) applies to Microsoft Word 2007 and 2010. You can find a version of this tip for the older menu interface of Word here: Processing Information Pasted from a PDF File.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Multiple Print Areas on a Single Printed Page

Want to print small, non-contiguous areas of your worksheet all on a single page? You might think that defining a ...

Discover More

Changing the Format of Existing Dates

There are a myriad of ways in which a date can be formattedâ€"day first, month first, number of digits in the year, etc. ...

Discover More

ExcelTips: The Macros (Special Offer)

ExcelTips: The Macros is the definitive reference on how to use macros in Excel. You can stay at the top of your ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (ribbon)

Inserting a Copyright Mark

One of the most common symbols that can be added to a document is the copyright mark. This tip examines several ways you ...

Discover More

Moving Text without Affecting the Clipboard

Want a quick and easy way to move text (or other document elements) from one place to another in your document? Check out ...

Discover More

Getting Rid of Manual Paragraph Numbering

Word includes a tool that allows you to easily apply automatic numbering to your paragraphs. You may be editing a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 3 + 3?

2019-02-27 13:44:35

Anthony Warner

VBA did not like two statements in this macro:
Dim MyData As DataObject

and

Set MyData = New DataObject

Apparently DataObject is not a valid option. I'd like to use this macro , as it would be very helpful for work I do. Can you help? I looked for the Microsoft forms library and it is not there available. I use Word 2016.

Thanks,
Anthony


2015-05-15 12:01:18

LBerkowitz

There are many free .pdf utilities available. I have been using PDF995 successfully and without any problems for several years now. Once installed, it becomes one of the printer options in Print Preview and Print. Here is a link:

http://www.pdf995.com/download.html


2015-05-15 10:58:23

Linda Putz

How do I convert a PDF document to Word 2013? without using macros?
Thank you!


2015-04-03 19:15:06

Kevin

In the VBA Editor, choose Tools | References.
I can confirm there is a check mark next to these.
Microsoft Office 14.0 Object Library (As per awyatt 20 Feb 2014).
Microsoft Word 14.0 Object Library.
Both of the above were already checked prior to investigation.
Unable to find one like George 2 Apr 2015 makes note of, Microsoft Office XX Forms Library. Thanks George but the Excel ref doesn't help me either.


2015-04-02 10:36:25

George

The DataObject is a Micrsoft Forms object. Try making sure that the Microsoft Forms Object Library is referenced in your macro project:

In the VBA Editor, choose Tools | References. Scroll through the list of available references, looking for one named Microsoft Office XX Forms Library. (The XX will vary depending on the version of Word being used.) Make sure there is a check mark next to the library, then click OK.


2015-04-02 10:33:18

Glenn Case

For those of you struggling with the DataObject reference:

A quick internet search turned up the info that you need to include a reference to the Microsoft Forms 2.0 Object Library, same as recommended by Mark Biegert in the comments to this tip. To add this, from the VBA Editor select Tools and References, and choose it from there (ensure you are not in Debug mode, or the References choice will be grayed out.) If it is not a choice in the list, then use the Browse button and search for "FM20.dll" (likely in your System32 directory.) Add that, and it should work.

For further info, see the site below:

http://www.excelforum.com/excel-programming-vba-macros/353942-how-do-i-reference-dataobject.html

Hope this helps.


2015-04-01 18:51:35

Kevin

They all (Including George) stop at the very start on this line.
Dim MyData As DataObject


2015-03-31 09:00:00

George

Here is a macro that handles vbCr and vbLf in the clipboard, and inserts a space at the end of lines.
Sub PastePDFClean()
' modified 31 March 2015
Dim MyData As DataObject
Dim sTextIn As String
Dim x As Integer
Dim y As Integer

Set MyData = New DataObject
MyData.GetFromClipboard
sTextIn = MyData.GetText

' replace carriage returns with spaces
x = InStr(sTextIn, vbCr)
y = 1
While x > 0
sTextIn = Left(sTextIn, x - 1) & " " & Mid(sTextIn, x + 1)
y = x + 1
x = InStr(y, sTextIn, vbCr)
Wend

' remove line feeds following spaces
x = InStr(sTextIn, " " & vbLf)
y = 1
While x > 0
sTextIn = Left(sTextIn, x) & Mid(sTextIn, x + 2)
y = x
x = InStr(y, sTextIn, " " & vbLf)
Wend

' remove remaining line feeds
x = InStr(sTextIn, vbLf)
y = 1
While x > 0
sTextIn = Left(sTextIn, x - 1) & Mid(sTextIn, x + 1)
y = x
x = InStr(y, sTextIn, vbLf)
Wend

Selection.TypeText sTextIn
Set MyData = Nothing
End Sub


2015-03-31 06:34:22

Christy

Sorry Allen, I should have specified that I already did all the things all the comments suggested and I still get the error in Word 2013.


2015-03-30 21:45:40

Kevin

Microsoft Office Object Library is referenced on my system.
"Compile error" User defined type not defined. continues to occur.


2015-03-30 11:13:17

awyatt

Christy: I'm the site owner and I have responded with a "fix." See my comment, below, dated 20 Feb 2014.

-Allen


2015-03-30 10:49:18

Christy

Disappointed to receive this tip on 3/30/15 and find the issue referenced above is still occuring and the site owners haven't responded on a fix.
{Keep receiving error "Compile error" User defined type not defined
MyData as DataObject}
Why continue to publish a tip that doesn't work for some people without addressing the error?


2015-03-30 09:15:10

Mark Biegert

I actually had to reference the Microsoft Forms 2.0 Object Library to make it work for me.


2015-03-30 07:14:47

LBerkowitz

Provided that the original .pdf is the sort that permits copying.


2015-01-12 17:43:42

Donald Cooley

Why not just convert the PDF to Word?


2014-03-04 20:11:15

Terence

Still have same problem


2014-02-20 07:36:14

awyatt

Try making sure that the Microsoft Office Object Library is referenced in your macro project:

In the VBA Editor, choose Tools | References. Scroll through the list of available references, looking for one named Microsoft Office XX Object Library. (The XX will vary depending on the version of Word being used.) Make sure there is a check mark next to the library, then click OK.

-Allen


2014-02-20 01:32:47

Terence

When attempting to run this macro I get:
Keep receiving error "Compile error" User defined type not defined
MyData as DataObject


2013-02-15 12:49:10

gary

Keep receiving error "Compile error" User defined type not defined
MyData as DataObject

How can I get this code to reun


2012-04-24 05:32:20

Dawei

Does not work for all Pdfs, have modified

Sub PastePDFClean()
Dim MyData As DataObject
Dim sTextIn As String
Dim x As Integer
Dim y As Integer

Set MyData = New DataObject
MyData.GetFromClipboard
sTextIn = MyData.GetText

x = InStr(sTextIn, vbCr)
y = 1
While x > 0
sTextIn = Left(sTextIn, x - 1) & " " & Mid(sTextIn, x + 2)
' MsgBox sTextIn
y = x + 1
x = InStr(y, sTextIn, vbCr)
Wend

Selection.TypeText sTextIn
Set MyData = Nothing
End Sub


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.