• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1539
  • Last Modified:

MS Word Search and Replace

Here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoreplacetext.asp MSDN says:

' Visual Basic
Friend Sub SearchReplace()
    With ThisApplication.Selection.Find
        .ClearFormatting()
        .Text = "find me"
        With .Replacement
            .ClearFormatting()
            .Text = "Found"
        End With
        .Execute(Replace:=Word.WdReplace.wdReplaceAll)
    End With
End Sub

Ok so I did that exactly, but because of strong names and such, I had to declare my own instance of Word:

Dim ThisApplication as MyWordDLL.Application // I know this initializes correctly

The problem I think is with this line:

        .Execute(Replace:=Word.WdReplace.wdReplaceAll)

So I tried variations of it, since the documentation is very detailed.

        .Execute(Replace:=ThisApplication.WdReplace.wdReplaceAll) // WdReplace is not an method or property of ThisApplication

Actually, WdReplace is an enumeration, as well as wdReplaceAll. Any ideas?

Thanks in advance.
0
drakkarnoir
Asked:
drakkarnoir
  • 6
  • 5
1 Solution
 
JR2003Commented:
The best way to find out what parameters you need is to go into Word and record a macro doing roughly what you want to do. Then go into the macro editor and look at the code that has been generated. This will show you how to use all the functions etc. within Word. I just did a find replace and got this code generated. It's not much effort to then add the wordApp object defined in you program to make the code work from within VB.

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "this"
        .Replacement.Text = "that"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

0
 
drakkarnoirAuthor Commented:
So wait, basically all that I have to do is then:

Dim ThisApplication as Word.Application
 ThisApplication.Selection.Find.ClearFormatting
    ThisApplication.Selection.Find.Replacement.ClearFormatting
    With ThisApplication.Selection.Find
        .Text = "this"
        .Replacement.Text = "that"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    ThisApplication.Selection.Find.Execute Replace:=wdReplaceAll
0
 
iboutchkineCommented:
>>.Execute(Replace:=ThisApplication.WdReplace.wdReplaceAll) // WdReplace is not an method or property of ThisApplication Actually, WdReplace is an enumeration
   
You are correct,. THis is an enumeration. Open word VB editor, type this expression and check the numeric value during debugging. then replace wdReplace,,, with this number
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
JR2003Commented:
drakkarnoir

If you include a reference in you project (from the Tool/References menu option in VB) to "Microsoft n.n Word Object Library" you should have access to all of Words enumerated types from within your VB project. If not got a reference, because you create you Word.Application using a CreateObject command, you just have to replace the enumeration names with the actual values.

wdReplaceAll = 2
wdFindContinue = 1
0
 
drakkarnoirAuthor Commented:
That being said...it should be:

>>.Execute(Replace:=ThisApplication.WdReplace.2)

?
0
 
drakkarnoirAuthor Commented:
Or should it be:

Selection.Find.Execute Replace:=2

Or neither?
0
 
JR2003Commented:
Yes,
Selection.Find.Execute Replace:=2
is correct
0
 
drakkarnoirAuthor Commented:
Ok I'll try, be right back with results :)
0
 
drakkarnoirAuthor Commented:
I just ran into another problem, I will post the other question and after that is answered, I will accept an answer here...turns out my assembly maybe all messed up.
0
 
JR2003Commented:
from VB
    Selection.Find.Execute , , , , , , , , , , 2

may be the correct way to call it.
instaead of
Selection.Find.Execute Replace:=2
0
 
drakkarnoirAuthor Commented:
Blah, VS.NET keeps adding ()'s, so I get:

ThisApplication.Selection.Find.Execute(, , , , , , , , , , 2)
0
 
JR2003Commented:
ok, no worries try creating a boolean called bFound and setting this to the result of the execute statement:

Dim bFound As Boolean

    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "s"
        .Replacement.Text = "b"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
   bFound = Selection.Find.Execute(, , , , , , , , , , 2)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now