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 March 30, 2015)

19

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.

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

Removing All Bookmarks

Need to get rid of a lot of bookmarks all at once? Word doesn't provide a way to do it, but you can use the short macro in ...

Discover More

Pulling Tables Back Into View

If you make structural changes to your table by adding new columns here and there, you could easily end up with a table that ...

Discover More

A Handy Pruner for Your Toolshed

When working with trees, bushes, and brush, the right pruner can make quick work of the tasks you need to do. The Corona ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More WordTips (ribbon)

Turning Off Word's Second Guessing with Quote Marks

When you type quote marks in a document, Word normally changes them to Smart Quotes. They look better on a printout, but Word ...

Discover More

Sticking with the Dashes

By default, dashes don't "stick" to the text that follows them. Here's one way around this normal formatting convention.

Discover More

Removing Breaks

Word allows you to add several types of "breaks" into your document. If you later want to remove any of them, you can use ...

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 8 - 2?

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.