Consulting

Results 1 to 7 of 7

Thread: Find.execute - Get sentence of match.

  1. #1

    Find.execute - Get sentence of match.

    Hullo,

    I'm using a range's Find.execute to loop through all the matchs of a word in a document. What I want to do is retrieve the sentence, or if this isn't possible the line or paragraph, that surronds the matched word. So if I'm searching for 'chips' in a document, and it occurs in the sentence 'I like Fish and Chips.', then I want the string 'I like Fish and Chips'.

    All help gratefully received - many thanks for your help!

    [vba]
    Set rngDoc = ActiveDocument.Content
    While blContinue
    rngDoc.Find.Execute strSearchWord, False, True

    If rngDoc.Find.Found = True Then
    'Code to get sentence goes in here

    intCount = intCount + 1
    blContinue = True
    Else
    blContinue = False
    End If
    Wend
    [/vba]

  2. #2
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    This is the way I would do it.

    [vba]
    Sub FindSentence()
    Dim aDoc As Document
    Dim SearchRng As Range
    Dim SearchString As String
    Dim FoundString As String
    Dim Counter As Integer
    Set aDoc = ActiveDocument
    Set SearchRng = aDoc.Range

    SearchString = "quick brown"
    Do
    With SearchRng.Find
    .ClearFormatting
    .Text = SearchString
    .Forward = True
    .Wrap = wdFindStop
    .Execute
    End With
    If SearchRng.Find.Found Then
    SearchRng.Select
    'You have to use Selection because
    'wdsentence is not available in Range
    With Selection
    .Expand wdSentence
    End With
    FoundString = Selection
    MsgBox FoundString & " " & Counter
    Counter = Counter + 1
    End If
    Loop Until Not SearchRng.Find.Found
    End Sub

    [/vba]

  3. #3
    Cheers - I found another method as well - I'll post it when I get home tonight.

  4. #4
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    "So if I'm searching for 'chips' in a document, and it occurs in the sentence 'I like Fish and Chips.', then I want the string 'I like Fish and Chips'. "

    OK.

    If you have 23 instances of "chips" - do you want:

    1. a separate message for each?
    2. a collection of sentences for each found, then a mesaage listing all of them?

    What, exactly, do you want to DO?

    I would do Tinbendr's code slightly differently.[vba]Dim r as Range
    Dim j As Long
    Set r = ActiveDocument.Range
    With r.Find
    Do While .Execute(FindText:="quick brown", _
    Forward:=True)=True
    j = j + 1
    with r
    .Select
    .Expand wdSentence
    Msgbox r.Text & " Counter: " & j
    .Collapse wdCollapseEnd
    End With
    Loop
    End With[/vba]I see no need for using a Document object, nor the FoundString variable, as the displayed text will be the range text anyway.

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Just as a possibly interesting point, I never declare integers anymore.

    Dim yadda As Integer

    VBA converts all Integer variables to Long variables automatically, whether you want it to, or not. It does this at the very start of parsing.

  6. #6
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    Just for prosperity's sake, I commented in my code that wdsentence is not available to Range. I was mistaken. Wdline is not available to Range.

    Quote Originally Posted by Tinbendr
    [vba]'You have to use Selection because
    'wdsentence is not available in Range
    With Selection
    .Expand wdSentence
    End With
    [/vba]

    David


  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Another example of the difference between Selection and Range.

    Selection is an expression of the GUI - therefore "line" is relevant, as line is derived from the printer driver. Line_A with printer driver X may have 213 characters, but 217 characters with printer driver Z. Selection - because it IS GUI - can say "expand to the limit of your current resolution as derived from the printer driver."

    Range is an expression of a specific numeric location within the document. It is NOT derived from the printer driver, so "line" does not mean anything. It has a Start and an End, and these are what they are, regardless of the printer driver and/or screen resolution.

Posting Permissions

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