Results 1 to 5 of 5
  1. #1
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Word search (MSACCESS 2003)

    I am trying to go through a word document against a table of keywords. At the moment I am getting a single word search to partly work, as it does not find the exact word sampled. eg if I look for the word Gunship, it could find the word Gun and say it's found it. Is there a way to tighten it up? Thanks

    Dim LWordDoc As String
    Dim WD As Object
    Dim strWordData As String
    Dim wanted As String

    wanted = "masters"

    LWordDoc = "c:LoggingTest.doc"


    Set WD = CreateObject(Class:="Word.Application")
    WD.Visible = False 'True

    'Open the Document
    WD.Documents.Open Filename:=LWordDoc, ReadOnly:=True

    WD.Selection.Find.ClearFormatting
    With WD.Selection.Find
    .Text = wanted
    .Forward = True
    End With

    WD.Selection.Find.Execute

    If WD.Selection.Find.Found Then

    ''WD.Selection.EndKey Unit:=wdLine, Extend:=wdExtend

    strWordData = WD.Selection.Range

    Me.Foundword.Caption = Left(strWordData, Len(strWordData))

    Else
    Me.Foundword.Caption = "Not Found"

    End If

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word search (MSACCESS 2003)

    I'm confused. If you search for "Gunship", Word will *NOT* find "Gun". On the other hand, if you search for "Gun", Word will find "Gunship" unless you have specified .MatchWholeWord = True within the With WD.Selection.Find ... End With block.

  3. #3
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word search (MSACCESS 2003)

    Hi Hans. At the moment if I look for Newtown it will find it, and if I look for town it finds it, but it's finding the same word each time.
    I will try and add MatchWholeWord = True and see if that cures it. Just going to try and see how I include the syntax. Many thanks

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Word search (MSACCESS 2003)

    If you want to find all instances of a word, you can loop:

    Do While WD.Selection.Find.Execute
    ...
    Loop

  5. #5
    4 Star Lounger
    Join Date
    Feb 2008
    Location
    United Kingdom
    Posts
    490
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Word search (MSACCESS 2003)

    Thanks Hans, I changed my code and things are working. I have incorporated a loop as you suggested to find the word count so all is now well. Thanks again for your help

    New code
    LWordDoc = "c:LoggingTest.doc"

    'Create an instance of MS Word
    Set WD = CreateObject(Class:="Word.Application")
    ' WD.Visible = True
    '
    ' 'Open the Document
    WD.Documents.Open Filename:=LWordDoc, ReadOnly:=True


    WD.Selection.Find.ClearFormatting
    With WD.Selection.Find
    .Text = "middlesex"
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = True
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    '
    WD.Selection.Find.Execute


    If WD.Selection.Find.Found Then


    ' WD.Selection.EndKey Unit:=wdLine, Extend:=wdExtend

    strWordData = WD.Selection.Range

    Me.Foundword.Caption = Left(strWordData, Len(strWordData))

    Else
    Me.Foundword.Caption = "Not Found"

    ' End If
    End If

Posting Permissions

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