PDA

View Full Version : Solved: Find text



RonNCmale
05-14-2013, 07:51 AM
Need code added to stop search when the end of document is reached.
Thanks in advance


Sub findInOpenDocuments()

Dim dlg As Word.Dialog

Dim doc As Word.Document

Dim f As Word.Find

Dim g As Word.Find

Dim strName As String

' this might not be quite enough...

strName = ActiveDocument.Name

Set dlg = Word.Dialogs(wdDialogEditFind)

dlg.Show

Set dlg = Nothing


' at this point, we have all the stuff we need to find/highlight

' the same texts in all the other documents

For Each doc In Word.Documents

If doc.Name <> strName Then

doc.Select

With Selection.Find

.ClearHitHighlight

.HitHighlight findtext:=Selection.Find.Text, highlightcolor:=WdColor.wdColorAqua

End With
End If

Next

End Sub

macropod
05-14-2013, 10:32 PM
Hi Ron,

Perhaps if you could explain what you're trying to achieve ...

If all you're trying to do is to highlight the selected text in all other open documents, you could use code like:
Sub findInOpenDocuments()
Dim dlg As Word.Dialog
Dim doc As Word.Document
Dim f As Word.Find
Dim g As Word.Find
Dim strName As String
Dim StrTxt As String
StrTxt = Selection.Text
If StrTxt = "" Then Exit Sub
strName = ActiveDocument.Name
For Each doc In Word.Documents
If doc.Name <> strName Then
With doc.Content.Find
.ClearHitHighlight
.HitHighlight findtext:=StrTxt, highlightcolor:=WdColor.wdColorAqua
.Execute
End With
End If
Next
End Sub

RonNCmale
05-15-2013, 05:57 AM
I'm trying to open the find dialog box and search through the word document for the word or phrase. I want the code to highlight the word and give me the option to continue to search, which my code does except when it completes going thru the document it continues from the beginning. I'm hoping to be able to stop the search when the last word is reached in the document.

fumei
05-15-2013, 03:17 PM
Why do you declare

Dim f As Word.Find

Dim g As Word.Find


but never use them

macropod
05-15-2013, 03:23 PM
Hi Highlights approach you're using doesn't allow you to highlight only part of the document. However, I have posted Find/Replace code before that does allow a user to selectively replace text. See, for example: http://answers.microsoft.com/en-us/office/forum/office_2010-customize/vba-restrict-find-and-replace-to-a-selection/85108164-27dd-40c6-9b08-62a516c97afe

RonNCmale
05-15-2013, 03:32 PM
Thanks, I think I can work with your code