Word search and replace

Started by arnovdw, October 11, 2015, 02:39:59 AM

Previous topic - Next topic

arnovdw

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

 :) 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) Select

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