PDA

View Full Version : [SOLVED] Open word document from excel and search and highlight in word document



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

mrojas
11-19-2013, 05:46 PM
What do you mean " I am now stuck on being able to highlight"? What is the code doing? Any error messages?

It appears that your loop may need to be modified to continue "Finding" any occurrence of the seeked word.

Something similar to this:

Selection.WholeStory
Do While Not Selection.End

Selection.Find.Execute sFindTExt
If Selection.Find.Found=true then
' do to text whatever is appropriate
End If
Loop