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.
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:
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.
Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!
For certain types of writing, you may want to make sure that the sentences in your document do not exceed a certain ...
Discover MoreSection marks are used regularly in the writings of some industries, such as in legal documents. If you need a way to ...
Discover MoreThe Track Changes tool can be a great asset when you are working on a document with others. It can also be a hassle if ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-05-17 03:40:18
Grant Gatland
I struggled with the compile errors and finally found a solution for that:
Open References in Word VBA
Click on Browse
Pick up this file and click Open
C:\WINDOWS\SYSTEM\FM20.DLL
or from
C:\Windows\SysWOW64\FM20.DLL
But when I run the macro, it types in the text with the original vbCR
I set up some watch point variables and ran in debug mode and everything is working correctly - sTextIn is modified to eliminate the vbCR's, but somehow they still appear when the final line of code is executed: Selection.TypeText sTextIn
Does anyone have an idea about this
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
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
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments