Deleting characters of any user-picked colour

M

Matt

Hello,

I need to make a macro that will clear (delete) characters of a given
colour from the active document.

I have made a macro that clears grey characters:
Application.Run MacroName:="TemplateProject.tw4winClean.Main"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "?"
.Font.Color = wdColorGray50
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With

Is it possible to make a macro that will display a dialog asking what
colour should be deleted?
E.g.:
- user presses Ctrl+M (for example)
- a dialog is displayed with colours
- user picks a colour (or colours)
- characters of these colours are deleted from the active document

Thank you in advance!!!

Maciej
 
M

Matt

http://gregmaxey.mvps.org/Highlighter.htmshows a way of popping up a
userform with a colour selector, albeit for a different purpose, which can
be adapted to your own requirements.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Can you help me in inserting it into my macro? How to insert it
instead of wdColorGray50 ????

Thanks!
Maciej
 
G

Graham Mayor

This is a little complicated to explain in text as it is not possible to
reproduce the userform in the forum. Word MVP FAQ - Userforms
http://word.mvps.org/FAQs/Userforms.htm explains the basics of userforms.

Greg (whose add-in I referred you to) is currently occupied with a personal
matter and may not have time to deal with this, but if you are not in a
hurry you *may* be able to interest him in modifying his add-in to delete
coloured text.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
H

Helmut Weber

Hi Matt,

as this is the beginners' group,
the following is a simple workaround,
which covers only the colors
that are offered initially in the "format font fontcolor" dialog,
not "more colors".

Sub Macro10()
Dim lRes As Long ' the result of the dialog
Dim lClr As Long ' colorindex
Dim rTmp As Range ' the document's range
Dim odlg As Dialog
Set rTmp = ActiveDocument.Range
Set odlg = Dialogs(wdDialogFormatFont)
With odlg
lRes = .Display
lClr = .Color ' the colorindex
End With
If lRes = 0 Then Exit Sub ' exit if cancelled
With rTmp.Find
.Font.ColorIndex = lClr
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub

The macro shows the format font dialog,
records the colorindex of the color selected,
checks whether the dialog was cancelled or not,
and if not, deletes the text of that color.

The trick about that is, that
dialog.color returns the colorindex,
not the color,
which gave me quite a headache,
since your posting appeared here.
--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
G

Greg Maxey

Matt, Helmut

I believe to get the actual color data you will have to look at a sample of
the colored text. Something like this:

Option Explicit
Type ColorComponents
Red As Long
Green As Long
Blue As Long
End Type

Sub FindDeleteColoredText()
Dim rTmp As Range
Dim Color As ColorComponents
Color = GetColorData
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Font.Color = RGB(Color.Red, Color.Green, Color.Blue)
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub

Function GetColorData() As ColorComponents
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim oColorLng As Long
Set oDoc = Documents.Add
ActiveWindow.Visible = False
Set oRng = oDoc.Range
Dim rVal As Long
Dim gVal As Long
Dim bVal As Long
oRng.InsertAfter "Sample Text"
oRng.MoveEnd wdCharacter, -1
Application.ScreenRefresh
oRng.Select
With Dialogs(wdDialogFormatFont)
If .Show = -1 Then
.Execute
ActiveWindow.Visible = False
oColorLng = oRng.Font.Color
GetColorData.Red = oColorLng Mod 256
GetColorData.Blue = Int(oColorLng / 65536)
GetColorData.Green = Int((oColorLng - (GetColorData.Blue * 65536) -
GetColorData.Red) / 256)
End If
End With
ActiveWindow.Visible = True
oDoc.Close wdDoNotSaveChanges
End Function

I suppose if text is that color already exists in the document then you
could modify the above to get the data from a selection of the text.



Helmut said:
Hi Matt,

as this is the beginners' group,
the following is a simple workaround,
which covers only the colors
that are offered initially in the "format font fontcolor" dialog,
not "more colors".

Sub Macro10()
Dim lRes As Long ' the result of the dialog
Dim lClr As Long ' colorindex
Dim rTmp As Range ' the document's range
Dim odlg As Dialog
Set rTmp = ActiveDocument.Range
Set odlg = Dialogs(wdDialogFormatFont)
With odlg
lRes = .Display
lClr = .Color ' the colorindex
End With
If lRes = 0 Then Exit Sub ' exit if cancelled
With rTmp.Find
.Font.ColorIndex = lClr
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub

The macro shows the format font dialog,
records the colorindex of the color selected,
checks whether the dialog was cancelled or not,
and if not, deletes the text of that color.

The trick about that is, that
dialog.color returns the colorindex,
not the color,
which gave me quite a headache,
since your posting appeared here.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
G

Greg Maxey

Matt, Helmut

Since one would assume that the colored text you want to find and delete
exists in the document you could select a bit of it and run:

Option Explicit
Type ColorComponents
Red As Long
Green As Long
Blue As Long
End Type

Sub FindDeleteColoredText()
Dim oRng As Range
Dim oSelRng As Word.Range
Dim Color As ColorComponents
Set oSelRng = Selection.Range
Color = GetColorData(oSelRng)
Set oRng = ActiveDocument.Range
With oRng.Find
.Font.Color = RGB(Color.Red, Color.Green, Color.Blue)
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub

Function GetColorData(ByRef oColorRng As Word.Range) As ColorComponents
Dim oColorLng As Long
oColorLng = oColorRng.Font.Color
With GetColorData
.Red = oColorLng Mod 256
.Blue = Int(oColorLng / 65536)
.Green = Int((oColorLng - (.Blue * 65536) - .Red) / 256)
End With
End Function

Helmut said:
Hi Matt,

as this is the beginners' group,
the following is a simple workaround,
which covers only the colors
that are offered initially in the "format font fontcolor" dialog,
not "more colors".

Sub Macro10()
Dim lRes As Long ' the result of the dialog
Dim lClr As Long ' colorindex
Dim rTmp As Range ' the document's range
Dim odlg As Dialog
Set rTmp = ActiveDocument.Range
Set odlg = Dialogs(wdDialogFormatFont)
With odlg
lRes = .Display
lClr = .Color ' the colorindex
End With
If lRes = 0 Then Exit Sub ' exit if cancelled
With rTmp.Find
.Font.ColorIndex = lClr
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With
End Sub

The macro shows the format font dialog,
records the colorindex of the color selected,
checks whether the dialog was cancelled or not,
and if not, deletes the text of that color.

The trick about that is, that
dialog.color returns the colorindex,
not the color,
which gave me quite a headache,
since your posting appeared here.

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top