Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching word documents

Status
Not open for further replies.

Domino2

Technical User
Jun 8, 2008
475
GB
Has anyone any idea how to search word documents for selected keywords or phrases? The code snippet just simply tells me the word has been found, but I am trying to get the line of text where the word is present. Thanks

searchString = "TOURS"

With wrdDoc
For p = 1 To .Paragraphs.Count
startRange = .Paragraphs(p).Range.Start
endRange = .Paragraphs(p).Range.End
Set tRange = .Range(startRange, endRange)
tString = tRange.Text
tRange.Find.Text = searchString
tRange.Find.Execute

If tRange.Find.Found Then
Me.List0.AddItem searchString & " Is present"
End If

Next
.Close
End With
wrdApp.Quit
 
Hi,

On the premise that you want to select the whole line on with the text is found, try something based on:
Code:
With wrdDoc.Content.Find
  .ClearFormatting
  .Text = SearchString
  .Replacement.Text = ""
  .Forward = True
  .Wrap = wdFindStop
  .Format = False
  .MatchCase = False
  .MatchWholeWord = False
  .MatchAllWordForms = False
  .MatchSoundsLike = False
  .MatchWildcards = False
  .Execute
  If .Found = True Then
    .Parent.Select
    wrdApp.Selection.Expand Unit:=wdLine
  End If
End With
If you only want the found text, delete 'wrdApp.Selection.Expand Unit:=wdLine'.

Cheers

[MS MVP - Word]
 
Many thanks, sorry for the delay getting back. I will give it a try, thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top