Solved

MS Word Search and Replace

Posted on 2004-08-19
12
1,470 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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