Results 1 to 8 of 8
  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 was unsure whether this should go in the Word forum or not. I have an access application where I am selecting a word, and trying to search a word document for the inclusion of the word. I am trying to get the text around the word so someone can read it to see if its applicable to what they are searching for. At the moment I am filling a listbox with what it finds, however the text extracted from the word document is either too short, or with a paragraph sometimes too long. Anybody got a better idea, thanks

    LWordDoc = "CocsTest.doc"

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

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

    WD.Selection.Find.ClearFormatting
    With WD.Selection.Find
    .Text = "Tour"
    .Forward = True
    End With

    WD.Selection.Find.Execute

    If WD.Selection.Find.Found Then

    Do While WD.Selection.Find.Found
    strWordData = ""
    WD.Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    strWordData = WD.Selection.Range
    WD.Selection.Collapse wdCollapseEnd
    Me.FoundWord.AddItem Left(strWordData, Len(strWordData) - 1)
    WD.Selection.Find.Execute
    Loop


    Else
    Me.TB.Caption = "Word 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)

    You could replace the line

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

    with

    WD.Selection.MoveStart Unit:=wdWord, Count:=-3
    WD.Selection.MoveEnd Unit:=wdWord, Count:=3

    This will select 3 words before the word and 3 words after it, so 7 words total. You can, of course, change -3 and 3 to another value.

  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)

    Thanks Hans. I modified the code, but seem to get strange results, attached is a picture which shows what is produced from the code below. The word it is trying to find is "Tour" Maybe its formatting characters in the word document?

    Do While WD.Selection.Find.Found

    strWordData = ""

    WD.Selection.MoveStart Unit:=wdWord, Count:=-1
    WD.Selection.MoveEnd Unit:=wdWord, Count:=8

    strWordData = WD.Selection.Range

    WD.Selection.Collapse wdCollapseEnd

    Me.FoundWord.AddItem strWordData

    WD.Selection.Find.Execute


    Loop
    Attached Images Attached Images

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

    Re: Word Search (MSACCESS 2003)

    I don't think it's the formatting - you're working with plain text strings here. It depends on the way the document is structured. Word will count periods, line breaks, etc. as "words" too. Take a good look at the document(s) to see what surrounds the word "Tour".

  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 pulled out the line in the document where Lord Hawk appears, and cannot explain why just Lord Hawk appears in my data of the last post. ?

    "and that was then followed by other tours, Lord Hawk. (04:14:09) "

    Is there any way to tighten finding a word, as Tour is wanted, and not Tours

    Would it be counting commas? Regards

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

    Re: Word Search (MSACCESS 2003)

    When I use that sentence and your code, I get

    other tours, Lord Hawk. (04:

    "other" is one word to the left.
    Word does include all punctiation in the word count, so the 8 words to the right are:
    1 "s" (to get to the end of the word "tours")
    2 comma
    3 "Lord"
    4 "Hawk"
    5 period
    6 parenthesis (
    7 "04"
    8 colon

    I have no idea why the word "other" isn't included when you try it.

  7. #7
    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 think there will probably too many variables between documents. Basically I am trying to do a database that contains links to word documents where certain keywords or names are in, and present the user with a snippet of the test to show where the occurence was in the document. If there was a way to convert the word document to text would I benefit from it? I think I will just have to play with what I have. Thanks for your help.

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

    Re: Word Search (MSACCESS 2003)

    You could write your own algorithm to select words, but it would be a lot of work.

Posting Permissions

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