Solved

MS Word Search and Replace

Posted on 2004-08-19
12
1,422 Views
Last Modified: 2011-09-20
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
Comment
Question by:drakkarnoir
  • 6
  • 5
12 Comments
 
LVL 18

Expert Comment

by:JR2003
ID: 11848292
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
 

Author Comment

by:drakkarnoir
ID: 11848756
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 11850810
>>.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
 
LVL 18

Expert Comment

by:JR2003
ID: 11851707
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
 

Author Comment

by:drakkarnoir
ID: 11853518
That being said...it should be:

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

?
0
 

Author Comment

by:drakkarnoir
ID: 11853526
Or should it be:

Selection.Find.Execute Replace:=2

Or neither?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:JR2003
ID: 11853623
Yes,
Selection.Find.Execute Replace:=2
is correct
0
 

Author Comment

by:drakkarnoir
ID: 11854455
Ok I'll try, be right back with results :)
0
 

Author Comment

by:drakkarnoir
ID: 11854876
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
 
LVL 18

Expert Comment

by:JR2003
ID: 11854936
from VB
    Selection.Find.Execute , , , , , , , , , , 2

may be the correct way to call it.
instaead of
Selection.Find.Execute Replace:=2
0
 

Author Comment

by:drakkarnoir
ID: 11856427
Blah, VS.NET keeps adding ()'s, so I get:

ThisApplication.Selection.Find.Execute(, , , , , , , , , , 2)
0
 
LVL 18

Accepted Solution

by:
JR2003 earned 500 total points
ID: 11856818
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now