PDA

View Full Version : Search Document for highlighted Text



dragonruler
02-10-2009, 08:40 AM
Ok so this what I have so far. I want to search the current open document for highlighted text then I want to hide that text with the given color. Right now Im just trying to get it to work, Ill deal with the form stuff later. So I can find the highlighted text and remove the color or change the highlighted color but I cant seem to figure out how to hide that text.



Private Sub CommandButton1_Click()
Const wdTurquoise = 3
Const wdRed = 5
Const wdBrightGreen = 4
Const wdNoHighlight = 0
Const wdBlue = 2


'Set objWord = CreateObject("Word.Application")
'objWord.Visible = True
'Set objDoc = objWord.Documents.Open("C:\Hidden.doc")
'Set objRange = objDoc.Range

Set objRange = ActiveDocument.Range

objRange.Find.Highlight = True
objRange.Find.Forward = True

Do While objRange.Find.Execute
If objRange.HighlightColorIndex = wdBrightGreen Then
'CANT USE THE NEXT STATEMENT FOR OBVIOUS REASONS
' But it gets the point across
objRange.Visible = False
End If
intPosition = objRange.End
objRange.Start = intPosition
Loop
ActiveWindow.ActivePane.View.Type = wdPrintView
End Sub

dragonruler
02-10-2009, 10:18 AM
ok never mind I figured it out, I had a brain fart. This works if you want to highlight something Bright Green then select the macro and it will make it hidden. Just change the colors for different things.


Private Sub CommandButton1_Click()
Const wdTurquoise = 3
Const wdRed = 5
Const wdBrightGreen = 4
Const wdNoHighlight = 0
Const wdBlue = 2

Set objRange = ActiveDocument.Range

objRange.Find.Highlight = True
objRange.Find.Forward = True

Do While objRange.Find.Execute
If objRange.HighlightColorIndex = wdBrightGreen Then
objRange.Select
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Font.Hidden = True
End If
intPosition = objRange.End
objRange.Start = intPosition
Loop
ActiveWindow.ActivePane.View.Type = wdPrintView
End Sub

CreganTur
02-10-2009, 12:37 PM
For future reference, the correct way to mark a thread as solved, so it shows as solved in the forum window, is to click Thread Tools at the top of the page, and then select Mark As Solved.

Thanks :thumb

fumei
02-10-2009, 01:37 PM
This can be better coded as:
Dim objRange As Range
Set objRange = ActiveDocument.Range

With objRange.Find
.Highlight = True
Do While .Execute(FindText:="", Forward:=True) = True
If objRange.HighlightColorIndex = wdBrightGreen Then
objRange.Font.Hidden = True
End If
Loop
At least if I understand what you are trying to do.

The above finds BrightGreen and makes it Hidden.

No use of Selection is needed.

geekgirlau
02-13-2009, 11:53 PM
Love that Gerry - very elegant!

I've been slowly wrapping my head around that dratted Selection issue. Inherited a mass of spaghetti code that used Selection for everything - it's taken forever but I've almost completely banished it.

fumei
02-16-2009, 12:05 PM
Oh my yes. Selection (probably from recorded macros which ONLY uses Selection) can make decidely massive spaghetti code.