Author Topic: Word search and replace  (Read 1300 times)

arnovdw

  • Newbie
  • *
  • Posts: 2
Word search and replace
« on: October 11, 2015, 02:39:59 am »
I need to do a simple search and replace in a Word document.

In VBA like this:
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "@adres@"
        .Replacement.Text = "new adres"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

 I can't figure it out myself, could someone with more knowledge give it a try?

arnovdw

  • Newbie
  • *
  • Posts: 2
Re: Word search and replace
« Reply #1 on: October 11, 2015, 05:47:21 am »
 :) I figured it out!

I found another example, on msdn under the Find object:

Set myRange = ActiveDocument.Content
myRange.Find.Execute FindText:="hi", ReplaceWith:="hello", _
    Replace:=wdReplaceAll

And with some effort I came to:

Code: Winbatch
oWORD  = ObjectOpen("Word.Application")
oDOCS = oWORD.Documents
oDOC  = oDOCS.Open("test.doc")

oWORD.visible=@true
oActiveDoc = oWORD.activedocument

; Word Constants
wdReplaceNone = 0
wdReplaceOne = 1
wdReplaceAll = 2

myRange = oActiveDoc.Content
myRange.Find.Execute(:: FindText="@adres@", ReplaceWith="test", Replace=wdReplaceAll)

etc
 

And it works !  ;D