Print Page | Close Window

Find ONLY particular Highlighted words - VBA

Printed From: www.exp-systems.com
Category: PDF reDirect
Forum Name: Programming
Forum Discription: VBA and Batch Tools to control PDF reDirect Pro
URL: http://www.exp-systems.com/Forum_exp/forum_posts.asp?TID=580
Printed Date: 28 Apr 24 at 4:25PM


Topic: Find ONLY particular Highlighted words - VBA
Posted By: vamshivemula
Subject: Find ONLY particular Highlighted words - VBA
Date Posted: 21 Feb 10 at 10:21PM
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


Croos-posting links:
http://www.exp-systems.com/Forum_exp/forum_posts.asp?TID=580
http://www.vbaexpress.com/forum/showthread.php?p=206338#post206338
http://www.thecodecage.com/forumz/members-word-vba-programming/181336-find-only-particular-highlighted-words-visual-basic-applications.html#post650165




Replies:
Posted By: Michel_K17
Date Posted: 22 Feb 10 at 12:45PM
Hi,

   This forum is for users of PDF reDirect Pro, and how to use the ActiveX component for that product in VBA and other languages.

   I think your question is for Word VBA, and should be addressed in a different forum such as [ http://www.xtremevbtalk.com/forumdisplay.php?f=14 - here ].

   Best regards,


-------------
Michel Korwin-Szymanowski
EXP Systems LLC


Posted By: vamshivemula
Date Posted: 22 Feb 10 at 1:57PM
Below is the solution for the above problem.  This solution has been provided by FUMEI (VBAX Expert) at http://www.vbaexpress.com/forum/showthread.php?p=206338#post206338.

Thanks a lot to FUMEI :-)

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
 



Print Page | Close Window