PDA

View Full Version : Find ONLY particular Highlighted words - VBA



vamshivemula
02-21-2010, 07:27 PM
Hi,

I need a help with VBA code where ONLY one particular color highlighted text is selected (for instance a word highlighted in BRIGHT Green (wdBrightGreen)) and the color is removed for the selected text. I used the below code to remove the highlight which follows after the find code, but the code I used find all the highlighted text irrespective of what color it is:


While Selection.Find.Execute = True
Selection.Range.HighlightColorIndex = wdNoHighlight
Wend

I need this in two ways. First is to select all Bright Green highlighted text and remove the highlight in one instance in whole document and the second is to find one word at time and remove only one time so that I can have proper control. I used the following macro code which does not allow me to select particular color but finds the text whichever is highlighted.


Public Sub callChoice()
If MyFrm.optWord.Value = True Then
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Highlight = True
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

With Selection
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseStart
Else
.Collapse Direction:=wdCollapseEnd
End If
.Find.Execute Replace:=wdReplaceOne
If .Find.Forward = True Then
.Collapse Direction:=wdCollapseEnd
Else
.Collapse Direction:=wdCollapseStart
End If
.Find.Execute
End With
ElseIf MyFrm.optDoc.Value = True Then
Unload MyFrm
Application.ScreenUpdating = False

Selection.WholeStory
Options.DefaultHighlightColorIndex = wdNoHighlight
Selection.Range.HighlightColorIndex = wdNoHighlight
MsgBox ("All the highlights in the Document are removed")
Selection.HomeKey Unit:=wdStory

Application.ScreenUpdating = True
End If
End Sub

lucas
02-21-2010, 10:06 PM
Select your code and hit the vba button to format your code for the forum.

I'll have to let someone else assist with your problem. Welcome to the board.

Simon Lloyd
02-21-2010, 11:40 PM
Cross posted here http://www.thecodecage.com/forumz/newreply.php?do=newreply&noquote=1&p=650165
Vamshivemula, please read the link in my signature.

vamshivemula
02-22-2010, 09:16 AM
Hi Simon,

I tried to post links of the coss-posting but as I am new it did not allow and got a message that my message count should be greater than 5 to post any links and my intent was never to hurt or waste anyone precious time.

Vamshi

fumei
02-22-2010, 09:47 AM
I can not attach anything at the moment, but the following removes ONLY BrightGreen highlights.
Sub Highlight()
Dim r As Range
Set r = ActiveDocument.Range

With r.Find
.Highlight = True
Do While .Execute(FindText:="", Forward:=True) = True
If r.HighlightColorIndex = wdBrightGreen Then
r.HighlightColorIndex = wdAuto
r.Collapse 0
End If
Loop
End With
End Sub


If you wish to ask the user each time a highlight is found (if they want to remove THAT one), then you need to add some sort of question, which is easily done.

vamshivemula
02-22-2010, 11:56 AM
Thanks a lot FUMEI :-)