0

I am trying to search through Word documents and try to find a certain character. The ± character to be precise. The code can find the character because I have it print to screen if it found it. But it is unable to replace the character.

I have even tried searching for a random string I knew was in the files such as "3" and replacing it with something random such as "dog". But nothing works. It still finds the characters but does not replace.

Option Explicit
Dim objWord, objDoc, objSelection, oFSO, folder, jj, file

Set objWord = CreateObject("Word.Application")
objWord.Visible = False: objWord.DisplayAlerts = False
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set folder = oFSO.GetFolder("C:\Users\Desktop\myFolder")

For Each file In folder.Files
        objWord.Documents.Open file.path, False, True ' path, confirmconversions, readonly

        Set objDoc = objWord.ActiveDocument
        Set objSelection = objWord.Selection

        objSelection.Find.Forward = True
        objSelection.Find.MatchWholeWord = False
        objSelection.Find.Text = ChrW(177)
        objSelection.Find.Replacement.Text = "ChrW(177)"

        objSelection.Find.Execute

        If objSelection.Find.Found = True then
                Wscript.echo "Character Found"
        End If
        objDoc.close
Next
objWord.Quit
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
Aaron G.
  • 3
  • 3

2 Answers2

0
 objSelection.Find.Execute

should be

 objSelection.Find.Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue 

(assuming you want to replace all occurrences

This code running inside a word module will replace all "cat" with "dog"

Sub test()
Dim objdoc As Document
Dim objSelection As Range
Set objdoc = ActiveDocument
        Set objSelection = Selection.Range

        With objSelection.Find
            .Forward = True
            .MatchWholeWord = False
            .Text = "Cat"
            .Replacement.Text = "Dog"

            .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue

            If .Found Then
                  Debug.Print "Found"
            End If
        End With
End Sub

However you're running in vbscript using late binding, so possibly there's a type issue - you're selection object may not be a range but a variant?

Harassed Dad
  • 4,669
  • 1
  • 10
  • 12
0

The problem is that the code in the question doesn't specify that a replacement should take place. This is set in the Execute method, as a parameter.

Since this is VBScript rather than VBA it's not possible to use the enumerations (such as wdReplaceAll). Instead, it's necessary to specify the numeric equivalent of an enumeration. VBA without enumerations...

objSelection.Find.Execute Replace:=2, Forward:=True, Wrap:=0

However, VBScript doesn't accept named arguments, so all the arguments need to be specified by position, with "empty commas" if nothing should be specified.

 objSelection.Find.Execute , , , , , , , 0, , , 2

To discover the numeric equivalent consult either the VBA Object Library (F2 in the VBA Editor), the Language Reference or use the Immediate Window (Ctrl+G) in the VBA Editor like this: ?wdReplaceAll then press Enter.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • I still get a compile error on that line saying "Expected Statement". I have tried everything to get it to run. Any ideas? – Aaron G. Aug 12 '19 at 15:31