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.
by Allen Wyatt
(last updated March 30, 2015)
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.
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!
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
By default, dashes don't "stick" to the text that follows them. Here's one way around this normal formatting convention.Discover More
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
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.