?
Solved

MS Word Search and Replace

Posted on 2004-08-19
12
Medium Priority
?
1,500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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!

 
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
 
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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

770 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