PDA

View Full Version : Solved: Highlight word is highlight?



dikdikdik
03-31-2009, 02:43 AM
Hi everyone. I use Record macro in Word to see the code help me to find word and highlight them. But the macro I record, it not run. Here is my record code


Selection.Find.ClearFormatting
With Selection.Find
.Text = "something"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.CorrectHangulEndings = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

Any one know about the issue can show me what is lack?
I still have stupid question. If I want to see "find and replace" form, how do I do. Because when I use macro, seem every form is hidden and run automatic.

lucas
03-31-2009, 08:40 PM
One way you might do this:
Option Explicit
Sub HighlightIt()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="something", Forward:=True) = True
r.HighlightColorIndex = wdYellow
Loop
End With
End Sub

Sub UnHighlightIt()
Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:="something", Forward:=True) = True
r.HighlightColorIndex = wdNoHighlight
Loop
End With
End Sub

dikdikdik
03-31-2009, 09:38 PM
Yeah, I see it. It is highlight function I consider to use as a simple solution. But what I really want is the same with function "highlight all item found in" check box in "Find and replace" form. Because I want my macro show "Find and replace" form and I can type the string I want to find or replace. May be you think why I choice a difficult way? The reason is I don't use directly word application. My way is
1. Run my software
2. Use my soft to call word automation, open word file, send something from my soft to word automation like : name path to open word file, some string to search or replace in word file. My soft use C++ to call word automation, but I myself can convert VB code to C++ code
3. See "Find and replace" form in word document. I want to check the string before execute find or replace function. I don't like it run hidden because I can't control the change data and the document can lose exactly.

Tasks 1,2 I have completed and I can open word file I need. I have replaced automatic some string too. But now I want improve my code, I want show form before I execute find or replace function.

I search more but see nothing about code of function "highlight all item found in" and show the form by macro

Anyone can help me???

lucas
03-31-2009, 09:58 PM
Would an input box work?

edit: I added a parameter(marked in red) to only find the whole word. If you don't use it it will highlight the "with" part of the word "without" if you are searching for the word "with"

Sub HighlightIt()
Dim r As Range
Dim mystring As String
mystring = InputBox("enter a word to find and highlight")
Set r = ActiveDocument.Range
With r.Find
Do While .Execute(findtext:=mystring, MatchWholeWord:=True, Forward:=True) = True
r.HighlightColorIndex = wdYellow
Loop
End With
End Sub


also to remove all highlights from the document:

Sub UnHighlightIt()
Dim r As Range
Set r = ActiveDocument.Range
r.HighlightColorIndex = wdNoHighlight
End Sub

dikdikdik
04-02-2009, 02:39 AM
Thank Lucas. I think I can use it. Now I will start to convert to be C++ code.