Find, Copy and Paste a Paragraph into a new Word Document
February 22, 2007 8:22 AM   Subscribe

Is there a way to automatically find, copy, and paste an entire paragraph into a separate document in Microsoft Word based on just the beginning of the paragraph?

What I want to do is create a Word document that contains only paragraphs that contain a starting term, say "MN:", from another Word document. I want to be able to do this automatically. Is there a way to do this?
posted by Jeff_Larson to Computers & Internet (10 answers total) 2 users marked this as a favorite
 
Word allows you to search within a document for things like carriage returns, and supports regular expression searching, so you should be able to create a macro that grabs everything between the starting and the next carriage return (something like ^n or ^p in Word's hidden-character lingo), and pastes it into the appropriate place.

i.e. The answer to your question is surely yes, and a little bit of fiddling around should turn up the method. Sorry I can't be more specifically helpful...
posted by waxbanks at 8:29 AM on February 22, 2007


A bit of googling around suggests the following procedure:

Turn on wildcards in the 'Find' box. Choose 'Highlight all occurrences in the main document'. Search on (for instance):

MN:*^13

(^13 is the code for end-of-paragraph when wildcards are turned on; in normal searches you can just type in ^p.)

You should end up with a bunch of paragraphs highlighted. Copy, then paste into another document. Dunno if that's sufficiently programmatic but it's a start in any case, I suppose. Note: on my Mac, this ^13 code doesn't seem to work properly. But the LazyWeb insists that it should work on Windows.
posted by waxbanks at 8:55 AM on February 22, 2007


Just so you're aware, regular expressions are not for the faint of heart. They're not obscenely difficult, but if you're not a programmer, you will look at a regex string like < ([dm][rs]{1,2})( ) and probably throw up in your mouth a little bit. (finds patterns of mr, ms, mrs, and dr without periods, btw.)br>
Pasting things into an entirely separate document is likely the purview of a macro, which I cannot help you with at all. Perhaps a quick alternative, like simply stripping the non MN:-prefixed paragraphs and saving that file as a new document itself might work, without having to deal with a macro?

In that case, you may have some luck getting started here, or with a much more detailed look at regex in word, here.

Regular expressions are extremely powerful and very flexible. The price you pay is that they're also a nightmare to parse visually, because your brain isn't wired quite like that, especially if you don't program.

If you're in the mood for a bit of humor though, check out this comic strip, once you've figured it out.
posted by disillusioned at 9:06 AM on February 22, 2007


Whoa, disillusioned! I'd never heard of using regular expressions in Word! Thank you so much for the awesome links!!
posted by jasper411 at 9:21 AM on February 22, 2007


This should work:

Sub CopyPara

Selection.Find.ClearFormatting
With Selection.Find
.Text = "What You're Searching For"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.StartOf Unit:=wdParagraph
Selection.MoveEnd Unit:=wdParagraph
Selection.Copy
Documents.Add DocumentType:=wdNewBlankDocument
Selection.PasteAndFormat (wdPasteDefault)
End Sub

That macro will find the first instance of your text; you can probably figure out how to make it find them all.
posted by solid-one-love at 10:07 AM on February 22, 2007


How exaclty would I make the above find all matches? I'm working in Word 2000 and it doesn't have a "match all" option in the find dialog box (as far as I can tell).
posted by Jeff_Larson at 10:27 AM on February 22, 2007


In Word 2003 there is a checkbox for Highlight all items found in: Main Document

finds patterns of mr, ms, mrs, and dr without periods

Of course, it also finds drr, drs, ds, dsr, dss, mrr, msr, and mss. Another example of why it's sometimes not good to be too clever with the regexes.

posted by grouse at 10:56 AM on February 22, 2007


OK, give this a shot:

Sub CopyParas
Selection.Find.ClearFormatting
With Selection.Find
.Text = "The Text You Want to Find"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
Selection.StartOf Unit:=wdParagraph
Selection.MoveEnd Unit:=wdParagraph
sBigString = sBigString + Selection.Text
Selection.MoveStart Unit:=wdParagraph
Loop
Documents.Add DocumentType:=wdNewBlankDocument
Selection.InsertAfter (sBigString)
End Sub

This should copy every paragraph starting with your text to a string and then past the string into a new document. Replace the text after .Text as appropriate for your needs.

(I am assuming that you know how to -- or can figure out how to -- copy and use a macro via Tools --> Macro --> Visual Basic Editor.)
posted by solid-one-love at 11:00 AM on February 22, 2007 [1 favorite]


Wow. Awesome. Thanks!
posted by Jeff_Larson at 11:23 AM on February 22, 2007


you may also want to have a look at autohotkey.
posted by Baud at 2:48 PM on February 22, 2007


« Older So I've been wanting to go to ...   |  Can anyone recommend a reliabl... Newer »
This thread is closed to new comments.