PDA

View Full Version : Find.execute - Get sentence of match.



BunkMoreland
02-18-2008, 01:35 PM
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!


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

Tinbendr
02-18-2008, 07:17 PM
This is the way I would do it.


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

BunkMoreland
02-19-2008, 03:36 AM
Cheers - I found another method as well - I'll post it when I get home tonight.

fumei
02-19-2008, 11:23 AM
"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.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 WithI see no need for using a Document object, nor the FoundString variable, as the displayed text will be the range text anyway.

fumei
02-19-2008, 11:29 AM
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.

Tinbendr
02-29-2008, 06:37 AM
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.


'You have to use Selection because
'wdsentence is not available in Range
With Selection
.Expand wdSentence
End With

fumei
02-29-2008, 11:07 AM
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.