Changing the Font with a macro button on my toolbar

C

Charlie

I made this code so I could automatically decrease the font by 1 size, but it
does the Style "Normal". I'd like it do do the Style that is whereever I
leave my cursor. How would I change this code to do that? Thanks, Charlie

Sub DecreaseFont()
'
' This will decrease the font size of the style "Normal" by 1 point
'

Selection.Style = ActiveDocument.Styles("Normal")
With ActiveDocument.Styles("Normal").Font
.Size = .Size - 1
End With
End Sub
 
G

Greg

Charlie,

Not sure I follow. "IF" you want the font size for the Style at the
insertion point (cursor) to shrink by 1 pt. you can use:

Sub DecreaseStyleFont()
Dim oStyle As Style
Set oStyle = Selection.Style
oStyle.Font.Shrink
End Sub

to go the other way, use:

Sub IncreaseStyleFont()
Dim oStyle As Style
Set oStyle = Selection.Style
oStyle.Font.Grow
End Sub

If you simply want to shrink or grow selected text, use the built in
shortcuts

CTRL+Shitt+< and CTRL+Shitt+>
 
J

Jay Freedman

Hi Charlie,

Word already has built-in commands to increase and decrease the font size by
1 point, so you don't need a macro.

In the Tools > Customize dialog, click Format in the left-hand list and find
"Grow Font 1 pt" and "Shrink Font 1 pt" in the right-hand list. Drag them to
a toolbar or menu.
 
S

Sean McNamara

"At the cursor" is an ambiguous selection without looking
at what this really means. To get the functionality you
want, you need to decide what you really are asking for.
Here are a few ways to help you partition your thoughts:

1. Do you REALLY want to change an entire style,
permanently? Or are you just interested in changing the
font size of a particular piece of text that is by the
cursor?

2. "Text by the cursor" can mean a variety of things: one
character to the left of the cursor, one character to the
right, the nearest whole word (separated from other words
by spaces, tabs and carriage returns/linefeeds), or it can
even mean a whole line of text.

3. The Selection Object is actually a special type of
Range object. Whereas there can be an infinite number of
Range objects declared by the user, there can only be one
Selection object at any one time. This is because any time
you try to modify a Selection object (by redefining which
characters are actually selected), the Selection on the
page is immediately changed! When you modify what
characters are selected in a Range object, nothing changes
on the visual representation of the page.

Try this code to see what I mean:

Sub ExpandSelection()
Selection.Expand wdWord
End Sub

You should see the nearest word (to the right of your
cursor) selected on the page. HOWEVER,

Sub ExpandARange()
Dim r As Range
Set r = Selection.Range
r.Expand wdWord
MsgBox IIf(r Is Selection.Range,"This should not
happen","The Range object ""r"" is an abstracted area of
characters that isn't attached to the Selection object
anymore.")
End Sub


As you can see, even if you have a blinking cursor and
nothing "selected" (by dragging the mouse over something),
you still can get a Range object, if that makes any sense.
By default, anything you do in Word will automatically
attempt to adaptively apply operations "at the blinking
cursor" depending on where it is.

If there is no text selected and the cursor is...

- In the middle of a word:
The Selection object is a Range representing the word
that the cursor is inside of, where a "word" is denoted by
a string of characters separated from other words by
spaces, line breaks, carriage returns or tabs.

- Anywhere else:
The Selection object is a fake Range with zero
Characters inside of it. However, this Range will
automatically expand when you enter characters at this
particular insertion point. This produces the effect of
being able to "Type words in Bold text", rather than
having to type the word in regular text, then go back,
highlight the text, and Bold it. You don't have to design
your DecreaseFont procedure like that, but you CAN.

Here is a PROPOSED solution to your problem, but I'm not
sure if it's what you're after. Note that if you have a
selection that has multiple styles, this will do weird
things. Also, if you are dealing with the "Normal" style,
it will affect all text that is marked with the same style.

Sub DecreaseFontRight()
Dim r As Range, i%
If Selection.Characters.Count > 1 Then
Set r = Selection.Range
r.MoveEnd wdCharacter
Decrement CInt(r.Characters.Last.Style.Font.Size)
Else
Decrement CInt(Selection.Style.Font.Size)
End If
End Sub

Sub Decrement(ByRef i%)
i = i - 1
End Sub

If you don't care about styles but you just want to
decrease the font size, and you somehow stumbled upon a
reference to Styles (the VBA macro recorder?) then take
out the word "Style" from all of the above code.
 

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