jdooley3
11-11-2013, 11:24 AM
I am trying to search and highight inside a word document after opening it from excel. I have provided this portion of the code below. I am able to search and open the file of my choosing but I am now stuck on being able to highlight the text I want in the open document. Any and all help much appeciated.
'Open an existing Word Document from Excel
Dim FileToOpen
Dim appwd As Object
ChDrive "C:\"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Word Files *.docx (*.docx),")
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Error"
Exit Sub
Else
Set appwd = CreateObject("Word.Application")
appwd.Visible = True
appwd.Documents.Open Filename:=FileToOpen
End If
'This code will highlight the word(s) inside the " "
With appwd
Dim sFindText As String
sFindText = "IBM"
.Selection.ClearFormatting
.Selection.HomeKey wdStory, wdMove
.Selection.Find.ClearFormatting
.Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Selection.Range.HighlightColorIndex = wdYellow
Selection.MoveRight
Selection.Find.Execute
Loop
'Open an existing Word Document from Excel
Dim FileToOpen
Dim appwd As Object
ChDrive "C:\"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Word Files *.docx (*.docx),")
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Error"
Exit Sub
Else
Set appwd = CreateObject("Word.Application")
appwd.Visible = True
appwd.Documents.Open Filename:=FileToOpen
End If
'This code will highlight the word(s) inside the " "
With appwd
Dim sFindText As String
sFindText = "IBM"
.Selection.ClearFormatting
.Selection.HomeKey wdStory, wdMove
.Selection.Find.ClearFormatting
.Selection.Find.Execute sFindText
Do Until Selection.Find.Found = False
Selection.Range.HighlightColorIndex = wdYellow
Selection.MoveRight
Selection.Find.Execute
Loop