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 Items from a Context Menu

Context menus appear when you right-click an item in Excel. If you want to modify the menu that appears, the way to do so is ...

Discover More

Saving Information in a Non-Document Text File

Need to store some information in a plain text file? It's easy to do when you use a macro.

Discover More

Extracting INCLUDEPICTURE File Names

If you use the INCLUDEPICTURE field to add images to your document, you may love the macro in this tip. It allows you to pull ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Understanding Smart Cut and Paste

Editing is generally made easier by a feature that Word calls smart cut and paste. If you prefer, you can turn the feature ...

Discover More

Using Optional Hyphens

Adding hyphens to your document can affect the way in which Word wraps text from one line to the next. Optional hyphens, ...

Discover More

Inserting Foreign Characters

It is not unusual to need to insert foreign characters (often called diacritical marks) as part of your typing. Word provides ...

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 8Mpixels. 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 0 + 7?

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.