Word Hacks [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Word Hacks [Electronic resources] - نسخه متنی

Andrew Savikas

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید







Hack 42 Corral Basic Bullets and Numbering

Word offers 10 reliable, customizable, and,
best of all, predictable built-in styles for making bulleted and
numbered lists. So how come they've got nothing to
do with those decidedly unpredictable list
buttons on the toolbar? Here's how to hack some more
helpful buttons.

Admit it: the first time you ever clicked one of those little
list buttons, you were impressed. Bullets! Numbers! Indented and
aligned! How do they do that? Not very well, as
it turns out.

What happens when you press one of those buttons depends not on the
document you're using, nor on the template on which
the document is based. It doesn't even necessarily
depend on what happened the last time you
clicked the button. It depends on which one of the seven different
available list formats you last chose from the
FormatBullets and Numbering dialog, as shown in Figure 4-23.


Figure 4-23. When you click the Bullets button on the Formatting toolbar, it will apply the formatting selected here

And those seven formats? They aren't necessarily the
same seven each time. Which of the seven you
last chose is stored in the Windows registry, a massive internal
database Windows uses to store information. If you click the same
button, in the same document, on two different computers, it can
yield different results. Still impressed? It gets worse. Once
you've gone through the trouble of populating a long
document with lists by using the Bullets or Numbering toolbar
buttons, there's no easy way to globally change how
those lists are formatted.

Most Word pros avoid these
two
buttons like the Plague. They prefer to use a paragraph style to
include lists in a document. If you use
paragraph styles, you can modify every paragraph that uses a
particular style at the same time. For the same reason, many power
users eschew the Bold and Italic buttons in favor of the Strong and
Emphasis character styles (see [Hack #39] ).

Word provides a boatload of built-in list styles for you. In addition
to the 10 bullet and numbering styles, Word has 10 more for
nonnumbered lists and list-continuation paragraphs. The list styles
all come factory-set with some basic formatting (the
"List Bullet 2" style is indented
more than the "List Bullet" style,
for example). You can see a few of these styles in Figure 4-24. The five List Bullet styles, as
they're formatted by default, are shown in Figure 4-25.


Figure 4-24. Word includes several dozen built-in list styles


Figure 4-25. The built-in list styles are already indented proportionally

But even users who know all about built-in list styles rarely go so
far as to actually remove the list buttons from
the Formatting toolbar. So the buttons get used anyway, especially
for short documents and, unfortunately, for long documents you
thought were going to be short.

By intercepting some built-in commands [Hack #61],
you can reeducate those buttons (along with the Increase Indent and
Decrease Indent buttons) to apply Word's predictable
and reliable built-in list styles, which you can easily modify as
needed in your documents or templates.


4.17.1 Mapping the Styles to the Buttons


In addition to the
Bullets and Numbering buttons, this
hack also intercepts the Decrease Indent and Increase Indent buttons.
These four buttons, shown in Figure 4-26, sit
adjacent to each other on the formatting toolbar.


Figure 4-26. The Four Buttons of the Apocalypse

This hack will use those four buttons to selectively apply the 10
built-in bullet and numbering list styles (List Bullet, List Bullet
2, List Bullet 3, List Bullet 4, List Bullet 5, List Number, List
Number 2, List Number 3, List Number 4, and List Number 5).

When you implement this hack, the action taken when you click each of
the four buttons will depend on the style currently applied to the
selected text. For example, if you select text styled as
"List Bullet 2," the following will
happen when you click each of the four buttons:

Bullets


Change back to Normal


Numbering


Change to "List Number"


Increase Indent


Change to "List Bullet 3"


Decrease Indent


Change to "List Bullet"



To implement this hack, you must create a fairly lengthy description
of which style gets applied for which button. Fortunately (for you,
not the author), that's already been done, so the
code below will work straight out of the box, so to speak.


4.17.2 The Code


This hack is a combination of five separate macros: one that makes
the decisions and applies the formatting, and four others that
intercept the commands used by the four toolbar buttons. Each of the
macros used to intercept the buttons
"calls" the fifth macro, the one
that does the formatting, and
"tells" it which button was
clicked.

The selected text will always be in one of 11 possible states: either
one of the 10 list styles, or some other style
altogether. For each scenario, you can take four possible
actionsone for each button.

These are the four macros that intercept the buttons. Put these,
along with the BetterBulletsAndNumbering macro
that follows, in the template of your choice [Hack #50] .

Sub FormatBulletDefault( )
Call BetterBulletsAndNumbering(Selection, "Bullets")
End Sub
Sub FormatNumberDefault( )
Call BetterBulletsAndNumbering(Selection, "Numbering")
End Sub
Sub IncreaseIndent( )
Call BetterBulletsAndNumbering(Selection, "IncreaseIndent")
End Sub
Sub DecreaseIndent( )
Call BetterBulletsAndNumbering(Selection, "DecreaseIndent")
End Sub


These four macros must be named exactly as
shown, or Word won't use them in place of the
commands they're named after.

The next macro does the real work of deciding which style to use and
applying it to the selected text. To efficiently handle such a large
number of options, this code uses VBA's
Select Case statements, a much
neater alternative to a massive set of complex If... Then...
Else
statements.

Function BetterBulletsAndNumbering(ByRef sel As Selection, _
ByVal sButton As String)
' We'll convert the passed sButton string
' to a constant for more efficient code
Const cBULLETS = 1
Const cNUMBERING = 2
Const cINCREASE_INDENT = 3
Const cDECREASE_INDENT = 4
Dim DocStyles As Styles
Dim styBullet1 As Style
Dim styBullet2 As Style
Dim styBullet3 As Style
Dim styBullet4 As Style
Dim styBullet5 As Style
Dim styNumber1 As Style
Dim styNumber2 As Style
Dim styNumber3 As Style
Dim styNumber4 As Style
Dim styNumber5 As Style
Dim styBodyText As Style
Dim iButtonPressed As Integer
' A variable for looping through
' each paragraph in the selection
Dim para As Paragraph
Set DocStyles = sel.Document.Styles
Set styBullet1 = DocStyles(wdStyleListBullet)
Set styBullet2 = DocStyles(wdStyleListBullet2)
Set styBullet3 = DocStyles(wdStyleListBullet3)
Set styBullet4 = DocStyles(wdStyleListBullet4)
Set styBullet5 = DocStyles(wdStyleListBullet5)
Set styNumber1 = DocStyles(wdStyleListNumber)
Set styNumber2 = DocStyles(wdStyleListNumber2)
Set styNumber3 = DocStyles(wdStyleListNumber3)
Set styNumber4 = DocStyles(wdStyleListNumber4)
Set styNumber5 = DocStyles(wdStyleListNumber5)
' Assumes you want the "default" body text to be
' Normal style.
Set styBodyText = DocStyles(wdStyleNormal)
Select Case sButton
Case Is = "Bullets"
iButtonPressed = cBULLETS
Case Is = "Numbering"
iButtonPressed = cNUMBERING
Case Is = "IncreaseIndent"
iButtonPressed = cINCREASE_INDENT
Case Is = "DecreaseIndent"
iButtonPressed = cDECREASE_INDENT
End Select
For Each para In sel.Paragraphs
Select Case para.Style
' Paragraph is List Bullet
Case Is = styBullet1
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBodyText
Case Is = cNUMBERING
para.Style = styNumber1
Case Is = cINCREASE_INDENT
para.Style = styBullet2
Case Is = cDECREASE_INDENT
para.Style = styBodyText
End Select
' Paragraph is List Bullet 2
Case Is = styBullet2
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBodyText
Case Is = cNUMBERING
para.Style = styNumber2
Case Is = cINCREASE_INDENT
para.Style = styBullet3
Case Is = cDECREASE_INDENT
para.Style = styBullet1
End Select
' Paragraph is List Bullet 3
Case Is = styBullet3
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBodyText
Case Is = cNUMBERING
para.Style = styNumber3
Case Is = cINCREASE_INDENT
para.Style = styBullet4
Case Is = cDECREASE_INDENT
para.Style = styBullet2
End Select
' Paragraph is List Bullet 4
Case Is = styBullet4
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBodyText
Case Is = cNUMBERING
para.Style = styNumber4
Case Is = cINCREASE_INDENT
para.Style = styBullet5
Case Is = cDECREASE_INDENT
para.Style = styBullet3
End Select
' Paragraph is List Bullet 5
Case Is = styBullet5
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBodyText
Case Is = cNUMBERING
para.Style = styNumber5
Case Is = cINCREASE_INDENT
' Do Nothing
Case Is = cDECREASE_INDENT
para.Style = styBullet4
End Select
' Paragraph is List Number
Case Is = styNumber1
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet1
Case Is = cNUMBERING
para.Style = styBodyText
Case Is = cINCREASE_INDENT
para.Style = styNumber2
Case Is = cDECREASE_INDENT
para.Style = styBodyText
End Select
' Paragraph is List Number 2
Case Is = styNumber2
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet2
Case Is = cNUMBERING
para.Style = styBodyText
Case Is = cINCREASE_INDENT
para.Style = styNumber3
Case Is = cDECREASE_INDENT
para.Style = styNumber1
End Select
' Paragraph is List Number 3
Case Is = styNumber3
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet3
Case Is = cNUMBERING
para.Style = styBodyText
Case Is = cINCREASE_INDENT
para.Style = styNumber4
Case Is = cDECREASE_INDENT
para.Style = styNumber2
End Select
' Paragraph is List Number 4
Case Is = styNumber4
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet4
Case Is = cNUMBERING
para.Style = styBodyText
Case Is = cINCREASE_INDENT
para.Style = styNumber5
Case Is = cDECREASE_INDENT
para.Style = styNumber3
End Select
' Paragraph is List Number 5
Case Is = styNumber5
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet5
Case Is = cNUMBERING
para.Style = styBodyText
Case Is = cINCREASE_INDENT
' Do Nothing
Case Is = cDECREASE_INDENT
para.Style = styNumber4
End Select
Case Else
Select Case iButtonPressed
Case Is = cBULLETS
para.Style = styBullet1
Case Is = cNUMBERING
para.Style = styNumber1
Case Is = cINCREASE_INDENT
WordBasic.IncreaseIndent
Case Is = cDECREASE_INDENT
WordBasic.DecreaseIndent
End Select
End Select
Next para
End Function

This code has two important features:

Sometimes you might click one of the indent buttons when
you've selected text that isn't
part of a list. In that case, the macro just
"passes" the command on to Word,
which will indent as it would if the button had never been
intercepted.

If you select multiple paragraphs with different styles applied, the
code loops through and formats each paragraph in the selection
separately, using a For Each
loop [Hack #66] .



4.17.3 Running the Hack


Once you've placed these macros in an active
template, they'll spring into action when you click
any of the four toolbar buttons shown in Figure 4-26.

If you want to change the formatting of the bullets or numbering,
modify the corresponding built-in list style.


/ 162