Highlighting in MS-Office - Intranet Search - Highlight Search Topic in the Office Document

S

steve

Hi,we use a search engine to find search results in our intranet. I
dont know how we can highlight search string in a result if it is a
office document.

example:

search for "boat"
....
found a boattrip.doc

open the doc

see the word "boat" highlighted in the whole document

any ideas?

thanx a lot

steve
 
D

Dean Boerrigter

Open Word. "Alt-F11" for the Word Visual Basic Editor.
Insert a module. Paste the following code:


Private Sub FindTextAndHighlight()
'Finds text in Word Documents, opens and highlights
each occurance.
Dim i As Integer, cnt As Integer
Dim FN(500) As String, StringToSearchFor As String,
FilePath As
String

FilePath = "C:\"

StringToSearchFor = InputBox("Search for Text")

Call InitializeFileNamesArray(FilePath,
StringToSearchFor, FN, cnt,
True)

For i = 1 To cnt
Documents.Open FN(i)

Call HighlightsWord(StringToSearchFor, wdYellow,
False)
Next i

End Sub

Sub InitializeFileNamesArray(FilePath As String,
SearchString As String,
FileNamesArray() As String, NumFiles As Integer,
SearchSubfolders As
Boolean)
'Finds files in a filepath containing "SearchString".
'Returns the filenames in an array and a count of
files found.
Dim fs As Object
Dim i As Integer

Set fs = Application.FileSearch
With fs
Dim Path As String
.LookIn = FilePath

.TextOrProperty = SearchString
.MatchTextExactly = True

.SearchSubfolders = SearchSubfolders
.FileType = msoFileTypeWordDocuments

If .Execute > 0 Then
NumFiles = .FoundFiles.Count
For i = 1 To .FoundFiles.Count
FileNamesArray(i) = .FoundFiles(i)
Next i
Else
NumFiles = 0
End If
End With
End Sub

Sub HighlightsWord(Word2Find As String, Color1 As Long,
MatchCase As
Boolean)
' Highlights Selected Sentence containing "Word2Find".
Highlights it
"Color1" .

With Selection.Find
.ClearFormatting
Do While .Execute(FindText:=Word2Find, Forward:=True,
Format:=True,
MatchCase:=MatchCase, Wrap:=False) = True
Selection.Range.HighlightColorIndex = Color1
Loop
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top