Google Answers Logo
View Question
 
Q: visual basic question, probably easy ( No Answer,   2 Comments )
Question  
Subject: visual basic question, probably easy
Category: Computers
Asked by: mrbandrews-ga
List Price: $4.00
Posted: 08 Nov 2005 10:48 PST
Expires: 08 Dec 2005 10:48 PST
Question ID: 590604
Hi, I am re-familiarizing myself with VB after several years.  I am
trying to just open a Word document, search it, and replace text with
other text.

The following code does not work.  The search & replace execution does
not find the text "foo".  The message box pops up "False."  How come?

thanks...


        Dim oWord As Word.Application
        Dim oDoc As Word.Document

        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add("C:\program\test.doc")

        oDoc.Range.Select()
        oDoc.Range.Text = "foo foo foo"

        oDoc.Range.Find.ClearFormatting()
        oDoc.Range.Find.Replacement.ClearFormatting()

        With oDoc.Range.Find
            .Text = "foo"
            .Replacement.Text = "bar"
            .Forward = True
            .Wrap = Word.WdFindWrap.wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With


        oDoc.Range.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)
        MsgBox(oDoc.Range.Find.Found)

        oDoc.SaveAs("C:\program\test2.doc")
Answer  
There is no answer at this time.

Comments  
Subject: Re: visual basic question, probably easy
From: nejla-ga on 09 Nov 2005 12:46 PST
 
Hi mrbandrews,
This is your code with few modifications. It works fine with no error:

Dim oWord As Word.Application
Dim oDoc As Word.Document

Set oWord = CreateObject("Word.Application")
oWord.Visible = True
Set oDoc = oWord.Documents.Add("C:\program\test.doc")
        
Call oDoc.Range.Select
oDoc.Range.Text = "foo foo foo"

Call oDoc.Range.Find.ClearFormatting
Call oDoc.Range.Find.Replacement.ClearFormatting
       

While oDoc.Range.Find.Execute("Foo", , , , , , , , , "Bar")
   Call oDoc.Range.Find.ClearFormatting
Wend
        
oDoc.SaveAs ("C:\program\test2.doc")

Wish you success
Nejla
Subject: Re: visual basic question, probably easy
From: manuka-ga on 09 Nov 2005 17:42 PST
 
This actually turned out to be quite interesting.

If you step through your code with the debugger, and have a watch set
on oDoc.Range.Find, you'll notice that the values don't change - even
though, if you move the Execute statement into with With block, the
replacement operation actually succeeds!

The reason is that each separate invocation of oDoc.Range.Find looks
at a diferent Find object. (This makes sense, since it would be
mind-bogglingly inefficient to have a Find object defined for every
possible range in your document.) So the Execute line in your code is
being applied to a different Find object than the one you set all your
fields in, so it's just doing a default search for nothing. Similarly,
when you check the Found field you're doing it on a freshly-minted
Find object, so you're just getting the default value.

You need to declare a separate Find object:

Dim oFind as Find 
[...]
Set oFind = oDoc.Range.Find

And then replace each instance of oDoc.Range.Find with oFind. 

I tend to agree with nejla, though; it's easier to make one call to
Execute and specify all your arguments in the function call - though
nejla was a bit lazy and skipped some of the things you specified.
Also, there's no need for a While/Wend loop (as nejla used), just use
the Replace argument to specify a Replace All. In general it's best to
be careful and specify all the arguments you need, because some
arguments are carried over from the last Find operation if you don't
specify them.

If you're happy to do without named arguments in function calls, this
can replace your whole With block and Execute statement:

 oFind.Execute "foo", False, False, False, False, False, _
    True, wdFindContinue, False, "bar", wdReplaceAll

If you want named arguments, I'd do it much as you did (though with
oFind instead of oDoc.Range.Find, as discussed), except that I'd put
the Execute statement into the With block as well. There's no reason
to leave it outside.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy