View Full Version : Finding a string in a word doc

11-20-2003, 11:39 AM
I know I can open a word file using CreateObject("Word.Application"). But till now I've been unsuccessful in searching for a word in a word doc. I would like to write a function like this

Function IsWordPresent(sFileName, sWord)
End Function

which would return me a true if the string contained in sWord is present in the word document in sFileName.

Any ideas? Thanks.

11-20-2003, 03:52 PM
I've never looked at CreateObject("Word.Application"), so can't comment on that, but my (admittedly not very helpful) answer would be - can you not just open the doc as text using FileSystemObject, drop the content into a string and search it that way?

Roy Sinclair
11-20-2003, 04:01 PM
I just went into Word > Tools > Macro > Record New Macro (target the local document, not the NORMAL.DOT template to hold the new macro).

Then I performed a search. After the search completed, I stopped recording the macro. Tools > Macro > Macro1 > Edit gave me this:

Sub Macro1()
' Macro1 Macro
' Macro recorded 11/20/2003 by Roy Sinclair
With Selection.Find
.Text = "myword"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub

I gave you the whole procedure I followed since it's a quick way to get a lot of good examples about how to accomplish various tasks.