Find/Replace code not working - why? 
Author Message
 Find/Replace code not working - why?

Here's the code, running in VB:

        dim oApp as Word.Application
        dim sTextToFind as String
        dim sReplaceWith as String

        Set oApp = CreateObject("Word.Application")
        sTextToFind = "#NAME#"
        sReplaceWith = "Mike Smith"

        With oApp.Selection.Find
            .Forward = True
            .Wrap = wdFindContinue
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = sTextToFind
            .Replacement.Text = sReplaceWith
            .Execute
        End With

 When I run this in a debug session the value of the .Found property is
true, but the original text is not replaced.

Thanks in advance,

Mike



Tue, 06 Sep 2005 23:16:26 GMT  
 Find/Replace code not working - why?
Hi Mike,

AFAIK, you have to set a replace parameter of the Execute method, or you
will only find (not replace) your material.

Try
.Execute Replace:=wdReplaceOne
or
.Execute Replace:=wdReplaceAll

HTH


Quote:
> Here's the code, running in VB:

>         dim oApp as Word.Application
>         dim sTextToFind as String
>         dim sReplaceWith as String

>         Set oApp = CreateObject("Word.Application")
>         sTextToFind = "#NAME#"
>         sReplaceWith = "Mike Smith"

>         With oApp.Selection.Find
>             .Forward = True
>             .Wrap = wdFindContinue
>             .ClearFormatting
>             .Replacement.ClearFormatting
>             .Text = sTextToFind
>             .Replacement.Text = sReplaceWith
>             .Execute
>         End With

>  When I run this in a debug session the value of the .Found property is
> true, but the original text is not replaced.

> Thanks in advance,

> Mike



Tue, 06 Sep 2005 23:45:46 GMT  
 Find/Replace code not working - why?
Yep. That was it. Thanks.


Quote:
> Hi Mike,

> AFAIK, you have to set a replace parameter of the Execute method, or you
> will only find (not replace) your material.

> Try
> .Execute Replace:=wdReplaceOne
> or
> .Execute Replace:=wdReplaceAll

> HTH



> > Here's the code, running in VB:

> >         dim oApp as Word.Application
> >         dim sTextToFind as String
> >         dim sReplaceWith as String

> >         Set oApp = CreateObject("Word.Application")
> >         sTextToFind = "#NAME#"
> >         sReplaceWith = "Mike Smith"

> >         With oApp.Selection.Find
> >             .Forward = True
> >             .Wrap = wdFindContinue
> >             .ClearFormatting
> >             .Replacement.ClearFormatting
> >             .Text = sTextToFind
> >             .Replacement.Text = sReplaceWith
> >             .Execute
> >         End With

> >  When I run this in a debug session the value of the .Found property is
> > true, but the original text is not replaced.

> > Thanks in advance,

> > Mike



Wed, 07 Sep 2005 04:19:34 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Why error code=2 (file not found) on this code

2. Why does the following code not work?

3. this code does not work, why?

4. Help Please...Code does not work why?

5. Why code will not work?

6. Why does this piece of code work with Word 97 but not Word 2000

7. why will this code not work

8. Why this code does not work:

9. why the @#$@% does this code not work

10. Anyone know why this code only works in 32bit and not 16 bit vb 4

11. Why this code does not work properly?

12. Why does this code not work?

 

 
Powered by phpBB® Forum Software