I would like to create a document that automatically changes the font color
while we are typing. Could someone please help. Thanks a lot for your time.
Regards,
Sam
--
Go Bear
Hi Sam,
While you are typing what?
If certain words should be colored, you might look into formatted
AutoCorrect entries.
Format the word in the color you want, select it, then open "Tools >
AutoCorrect options".
Make sure the word appears both in the "Replace" and "With" fields, and
check the "formatted text" option.
Those formatted AutoCorrect entries are saved in Normal.dot though.
So if you don't always want to use them, you'd need to find a way to add or
remove them from Normal.dot as you need them... possibly with a macro.
Regards,
Klaus
Sam
--
Go Bear
Was half afraid that'd be the case ;-)
> I try to create a text editor using Microsoft word that can randomly
> change the color of any letter we just enter by a key press.
> I create this to make it more fun for kids to learn typing.
> So, I don't know a head the letters whose color will be changed.
> I need a way to capture the event of key press and process that
> event. Again, thank you very much.
A given letter should always have the same color? Or should they all be
random?
Either way, with simple VBA you'd need to write a short macro for each key
that changes the font color, and bind that macro to the key.
... Not *too* much work using cut'n'paste...
Another difficulty will be to choose a color randomly, but make sure it's
not too light (and not too boring).
To get you started, the letter "a" (and "A"):
Sub KeyA()
Selection.Font.Color = wdColorRed
Selection.TypeText "a"
Selection.Font.Color = wdColorAutomatic
End Sub
After you run the macro Sub BindKeys() below, each "a" will turn red as you
type, and the rest of the text stays black.
If the colors should change completely at random, you could try
Selection.Font.Color = Int(&H1000000 * Rnd())
instead of
Selection.Font.Color = wdColorRed
and use the same code in every Key macro.
As I said above, some colors might be hard to read though if the color is
too light.
Sub KeyShiftA()
' let's make "A" green:
Selection.Font.Color = wdColorGreen
Selection.TypeText "A"
Selection.Font.Color = wdColorAutomatic
End Sub
... and so on for each key.
It's simpler to add the KeyBindings by macro too:
Sub BindKeys()
' You only need to run this once to add the KeyBindings!
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
Command:="KeyA", _
KeyCode:=wdKeyA
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
Command:="KeyShiftA", _
KeyCode:=BuildKeyCode(wdKeyShift, wdKeyA)
End Sub
Since you'll want to clean up after the kids are done, a macro to unbind the
keys again:
Sub UnbindKeys()
' Run this once to stop the coloring
' (Looks for KeyBindings to macros that start with "Key")
Dim vMacroName As Variant
Dim myKB As KeyBinding
For Each myKB In Application.KeyBindings
If myKB.KeyCategory = wdKeyCategoryMacro Then
vMacroName = myKB.Command
vMacroName = Split(vMacroName, ".")
If left(vMacroName(UBound(vMacroName)), 3) = "Key" Then
myKB.Clear
End If
End If
Next myKB
End Sub
Regards,
Klaus
Function RandomColor() As Long
Dim Max As Integer
Max = 765 '
While Max > 650 ' sum of the three RGB colors
RandomColor = Int(&H1000000 * Rnd())
Max = (RandomColor And &HFF&) + (RandomColor And &HFF00&) / &H100 +
(RandomColor And &HFF0000) / &H10000
Wend
End Function
You'd use it like this in every of your Key macros:
Sub KeyA()
Selection.Font.Color = RandomColor
Selection.TypeText "a"
Selection.Font.Color = wdColorAutomatic
End Sub
Regards,
Klaus
Klaus
Function RandomColor() As Long
Dim optLuminance As Integer ' around 128
' Bigger values: lighter, smaller values: darker
Dim varLuminance As Integer ' allowed variation (about 50?)
' Bigger values: more variation in brightness.
optLuminance = 128
varLuminance = 50
Dim Luminance As Integer
Luminance = 0
While (Luminance < optLuminance - varLuminance) Or _
(Luminance > optLuminance + varLuminance)
RandomColor = Int(&H1000000 * Rnd())
Luminance = 0.3 * (RandomColor And &HFF&) + _
0.59 * (RandomColor And &HFF00&) / &H100 + _
0.11 * (RandomColor And &HFF0000) / &H10000
Wend
End Function
Did you try you code? Somehow, it didn't work for me. When I ran the
macro, a green letter "A" appeared in the document by itself. Then when I
tried to type another "A", nothing happened :( When you create the macro,
you bind it to Tool or Keyboard? Thanks a lot. Have a great weekend.
Regards,
Sam
--
Go Bear
"Klaus Linke" wrote:
> "CodeLabor" wrote:
> >
> > Thanks a lot for your response, Klaus. But that isn't what I try to do.
>
> Was half afraid that'd be the case ;-)
>
> > I try to create a text editor using Microsoft word that can randomly
> > change the color of any letter we just enter by a key press.
> > I create this to make it more fun for kids to learn typing.
> > So, I don't know a head the letters whose color will be changed.
> > I need a way to capture the event of key press and process that
> > event. Again, thank you very much.
>
> A given letter should always have the same color? Or should they all be
> random?
>
> Either way, with simple VBA you'd need to write a short macro for each key
> that changes the font color, and bind that macro to the key.
> .... Not *too* much work using cut'n'paste...
>
> Another difficulty will be to choose a color randomly, but make sure it's
> not too light (and not too boring).
>
> To get you started, the letter "a" (and "A"):
>
> Sub KeyA()
> Selection.Font.Color = wdColorRed
> Selection.TypeText "a"
> Selection.Font.Color = wdColorAutomatic
> End Sub
>
> After you run the macro Sub BindKeys() below, each "a" will turn red as you
> type, and the rest of the text stays black.
>
> If the colors should change completely at random, you could try
> Selection.Font.Color = Int(&H1000000 * Rnd())
> instead of
> Selection.Font.Color = wdColorRed
> and use the same code in every Key macro.
> As I said above, some colors might be hard to read though if the color is
> too light.
>
> Sub KeyShiftA()
> ' let's make "A" green:
> Selection.Font.Color = wdColorGreen
> Selection.TypeText "A"
> Selection.Font.Color = wdColorAutomatic
> End Sub
>
> .... and so on for each key.
You need to run the macro BindKeys once, to bind the macro to the key "a".
And if you want to stop the coloring, run the macro UnbindKeys once.
If you add more keys, you'd need to write the macros (Sub KeyB(), ...), and
add the keys to the BindKeys macro.
Copy/paste mostly, with a tiny bit of editing...
Have a great weekend too!
Klaus
"CodeLabor" <Code...@discussions.microsoft.com> schrieb im Newsbeitrag
news:CAF20ACD-FE2F-4760...@microsoft.com...