Find Text in another document

T

Terry

Hello
I have a Text box in my document (Textbox1).
What I need to do is to search through the paragraphs in another document
for that text. For each paragraphs that contains that text, I need it
copied/pasted into the current document.
How can I do this?
Ive been trying to find a way to have the selection.execute() loop from
paragraph to paragraph, but with no luck

Thank you so much
Terry V
 
M

Mark Tangard

[posted and emailed]


Terry,

As you may have figured out, even if this code worked, crawling
through a document one word at a time is impossibly slow. See
my earlier post for a solution to this question. But since you're
obviously trying very hard, here are some general tips for your
next attempt at using any of these VBA elements:

These lines:
Set myrange = .Words(x)
If myrange = ThisDocument.TextBox1.Text Then

will give Word a headache. You've defined myrange as a variant
but its first usage assigns it to a Words object (which is a
range). So you'll never be able to test to see if it equals
a the .Text expression (which is a string, not a range).

This line:
myrange.Range.Paragraph.Copy

Will befuddle it even more since you're trying to apply the
..Range property to a range. But the bigger error here is
that .Paragraph is not a property of Range. What you would
have wanted here (if you were gonna try & use this code, which
ya aren't, eh?) was

myrange.Paragraphs(1).Range.Copy

The .Paragraphs(1) means the first (here the only) paragraph
that myrange includes or touches.

In this line:

myrange.MoveUntil Cset:=ThisDocument.TextBox1.Text

The CSet doesn't give the range a *string* to find and move to.
It gives a set of characters, and the range will move until it
finds ANY of those characters. For example, if you have the
sentence:

Joan ate Frank's shoe.

and you begin with myrange at the beginning of the sentence,
the line:

myrange.MoveUntil CSet:="Frank"

will *not* move the range to the word 'Frank'; instead it will
move it to the 'a' in Joan, because that's the first character
it'll find that's among the characters F, r, a, n, or k.

And finally,

[document object].Content.Paste

would just overwrite the entire content of your document with
what you're pasting. You actually want to *append* the paragraph
you found. See my other post.

One thing that would help you a lot as you wade through VBA is
to be sure Auto List Members is checked (in Tools­> Options­>
Editor) (click that from the VBA editor, not from Word). That
gives you a clickable list of all the methods & properties that
are allowed to complete the line you're coding, as soon as you
press the period after the last element you entered. If it's
not on the list, you can't use it there. This would've flagged
the blooper at myrange.Range.Paragraph.Copy.

Hope this helps.
 
T

Terry

Mark
Thank you so much
With a little manipulation, it works great.
Is there a way to bold the word in the new document that was searched for?
Ex: If the user searches for the Word "Excel", the paragraphs are placed in
"Thisdocument". Is there any way to bold the Word "Excel" in Thisdocument?

Thank you so much
Terry V
 
M

Mark Tangard

Yeah, just add this at the end:

resultdoc.Activate
With Selection.Find
.Text = "TextToFind"
.Replacement.Text = "^&"
.Replacement.Font.Bold = True
.Wrap = wdFindContinue
.Forward = True
.Format = True
.MatchCase = False
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
End With

NOTE: Don't get attached to the ThisDocument object. It's not what
it seems and not what you want for this sort of work. When you're
working on the actual content (text) of a document, typically you
want to use either ActiveDocument or a document object variable like
the resultdoc or checkdoc variables I've used here. ThisDocument is
mainly used in more complex VBA, for example, when building custom
menus & toolbars.
 
T

Terry

Mark
I cannot thank you enough.
This works great.

BTW... with the "Thisdocument". My reason for using it is simply to tell
the code to come back to the document that the code is in.
Will I run into problems using "Thisdocument"? I use this because
Activedocument could be another document with the focus.


Again
Thank you so much Mark, you've been a great help

Terry V
 
M

Mark Tangard

Terry,

That's the reason you set an object variable for each doc, or at
least for any document you want to "come back to." Then when it's
time to change focus to a document, just use:

[DocumentObjectVariableName].Activate

For example, in the code way below, you'd presumably want:

resultdoc.Activate

And that would work no matter how many docs are open or which has
the focus. Generally in code that manipulates more than one doc,
you'll have a couple of Set statments at the beginning and at least
one or two .Activate statements elsewhere.

(If I'd written this myself, I probably would've put it in, but
when churning out code examples it's hard to strike a balance
between including everything you'd ever need -- which can hide
the meat of the solution under tons of oops-preventatives -- or
giving just the bare bones. [I swear, some of the code I write
ends up being 75% cautions & error-checking...!])
 

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