Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Location
    India
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Finding a string in a word doc

    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.

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    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?

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 11/20/2003 by Roy Sinclair
    '
        Selection.Find.ClearFormatting
        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
        Selection.Find.Execute
    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.
    Check out the Forum Search. It's the short path to getting great results from this forum.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •