Solved

MS Word Search and Replace

Posted on 2004-08-19
12
1,410 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

20 Experts available now in Live!

Get 1:1 Help Now