Extract paragraphs to another document

N

New to VBA

X-No-Archive: Yes


I have a long Word document and I need to extract and copy to another
document all the paragraphs containing the word Symbol. I have no problem
with the code to search for Symbol, extend the selection to include the
whole paragraph and copy it to another document, but after the first
occurence of the word Symbol, I don't know how to "go back" to the document
and continue with the search and copy to the new document.

Any suggestions would be appreciated.
 
J

Jonathan West

New to VBA said:
X-No-Archive: Yes


I have a long Word document and I need to extract and copy to another
document all the paragraphs containing the word Symbol. I have no problem
with the code to search for Symbol, extend the selection to include the
whole paragraph and copy it to another document, but after the first
occurence of the word Symbol, I don't know how to "go back" to the
document and continue with the search and copy to the new document.

Any suggestions would be appreciated.

Learn to love the Range object. Almost every element of a Word document has
a Range property. Broadly speaking, an object's Range property is the bit
that would be highlighted if that object were selected.

In addition, a Range object variable acts very much like a Selection, with a
couple of very useful advantages.

1. You can define as many Range objects as you want to. You can even define
an array of Ranges.

2. A range object remains associated with a document even if you activate
another document.

So, for your problem, where you are presently using the Selection to find
your paragraphs, define a range, and use that. It is as simple as defining a
range (call it myRange for instance), set it to the selection or whatever
part of the document you want to search, suing Set myRange = Selection.Range
or similar, and then where you are using With Selection.Find, use instead
With myRange.Find.

Then you could have an additional range variable called myWholePara. Once
you have found something, you can then point myWholePara to the entire
paragraph, with a line of code like this

Set myWholePara = myRange.Paragraphs(1).Range

You can now copy myWholePara elsewhere. myRange hasn't moved, it is still
marking the found word, and so you can do another find to find the next
example.
 
N

New to VBA

X-No-Archive: Yes

Well, I really didn't want to learn to love ranges, but there are a couple
of very interesting messages in the newsgroup mentioning ranges vs.
selections, so I guess I'll have to :)

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top