paragraph numbering

J

J.I.B.

I am somewhat new to VB programming and would like some help to write a VB
macro in Word. Hopefully someone can provide help.

I need to number paragraphs of a specific style. The number format must be
[0000], [0001], ... [0010], ...[0100]

There needs to be a tab at 0.7" for these numbered paragraphs, and the
entire number field needs to be bold.

The normal numbering commands within Word work fine until you get to para
100, where it becomes [00100] instead of [0100].

Thanks.
 
D

Doug Robbins - Word MVP

Use a { seq number \# "0000" } field construction

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
J

J.I.B.

I have tried using the sequence field described. The problem is that this
field needs to be inserted (copied and pasted) manually before each
paragraph. I have not discovered a way of automating this for specific
styles.

Additionally, there are some subheaders which occur throughout the document
which should not be numbered. Fore example:

Title
[0001] text
[0002] text
Header
[0003] text]

Doug Robbins - Word MVP said:
Use a { seq number \# "0000" } field construction

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

J.I.B. said:
I am somewhat new to VB programming and would like some help to write a VB
macro in Word. Hopefully someone can provide help.

I need to number paragraphs of a specific style. The number format must
be
[0000], [0001], ... [0010], ...[0100]

There needs to be a tab at 0.7" for these numbered paragraphs, and the
entire number field needs to be bold.

The normal numbering commands within Word work fine until you get to para
100, where it becomes [00100] instead of [0100].

Thanks.
 
J

Jean-Guy Marcil

J.I.B. was telling us:
J.I.B. nous racontait que :
I have tried using the sequence field described. The problem is that
this field needs to be inserted (copied and pasted) manually before
each paragraph. I have not discovered a way of automating this for
specific styles.

Additionally, there are some subheaders which occur throughout the
document which should not be numbered. Fore example:

Title
[0001] text
[0002] text
Header
[0003] text]
Here is what you can do:

1)
Create the SEQ field, add a tab right after this field, select only the
field and the tab (not the ¶ or any other character)
2)
Create an AutoText based on the selection and call it "Num" (Of course, use
whatever name you want): Insert > AutoText... > AutoText > (give the name) >
OK.
3)
Create a style with the parameters you want (Tab or hanging indent at 0.7",
font, space before/after, etc.) Call this style the same name as the
AutoText, "Num" in this example.
4)
Do Alt-F11 to get to the VBA editor.
In the Normal Project, add a module (Right click the Normal Project heading
in the project pane on the left - View > Project Explorer if you cannot see
it) and choose "Module". Then in the code pane on the right for this module,
paste the following code:
'_______________________________________
Option Explicit
Private Const strStyle As String = "Num"

'_______________________________________
Sub ApplyNum()

Dim rgePara As Range

Set rgePara = Selection.Range.Paragraphs(1).Range

With rgePara
If .Style = "Num" Then
MsgBox "The current paragraph style is already """ _
& strStyle & """.", vbExclamation, "Cancelled"
Exit Sub
End If
.Collapse wdCollapseStart
.Style = strStyle
.InsertAfter strStyle
.InsertAutoText
.Fields(1).Result.Font.Bold = True
.Collapse wdCollapseEnd
.Select
End With

End Sub
'_______________________________________

'_______________________________________
Sub RemoveNum()

Dim rgePara As Range

Set rgePara = Selection.Range.Paragraphs(1).Range

With rgePara
If .Style <> strStyle Then
MsgBox "The current paragraph style is not """ _
& strStyle & """.", vbExclamation, "Cancelled"
Exit Sub
End If
.Fields(1).Result.Delete
.Characters(1).Delete
.Style = "Normal"
End With

End Sub
'_______________________________________
5) Close the VBA Editor Window
6) Tools > Customize > Commands tab > Keyboard... button
7) In the category on the left, select "Macros"
8) IN the list on the Right, select ApplyNum
9) Click inside the "Press shortcut key:" area
10) On the keyboard, do "CTRL-1"
11) Click on the "Assign" button (Of course, use whatever shortcut key
combination)
12) IN the list on the Right, select RemoveNum
13) Click inside the "Press shortcut key:" area
14) On the keyboard, do "SHIFT-CTRL-1"
15) Click on the "Assign" button (This is why I used CTRL-1, so that I can
now do SHIFT-CTRL-! to undo, but you could use CTRL-2 here. Of course, if
you already use CTRL-1 for its default shortcut, find another key
assignment)
16) Click on "Close" twice

Now all you have to do is CTRL-1 to apply the style, or SHIFT-CTRL-1 to
"un-apply" it.

If you don't like keyboard shortcuts, you can create toolbar buttons
instead.

Of course, if you have more than one document using this, you may want to
create a template and store all the customization in that template instead
of using Normal

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

J.I.B.

That partially did what I needed. When I run the macro, it makes a line
number as needed. But when I am done typing the paragraph and hit Enter, the
next line does not have the number, but is still in the correct style. Did I
miss something?

Thanks.

Jean-Guy Marcil said:
J.I.B. was telling us:
J.I.B. nous racontait que :
I have tried using the sequence field described. The problem is that
this field needs to be inserted (copied and pasted) manually before
each paragraph. I have not discovered a way of automating this for
specific styles.

Additionally, there are some subheaders which occur throughout the
document which should not be numbered. Fore example:

Title
[0001] text
[0002] text
Header
[0003] text]
Here is what you can do:

1)
Create the SEQ field, add a tab right after this field, select only the
field and the tab (not the ¶ or any other character)
2)
Create an AutoText based on the selection and call it "Num" (Of course, use
whatever name you want): Insert > AutoText... > AutoText > (give the name) >
OK.
3)
Create a style with the parameters you want (Tab or hanging indent at 0.7",
font, space before/after, etc.) Call this style the same name as the
AutoText, "Num" in this example.
4)
Do Alt-F11 to get to the VBA editor.
In the Normal Project, add a module (Right click the Normal Project heading
in the project pane on the left - View > Project Explorer if you cannot see
it) and choose "Module". Then in the code pane on the right for this module,
paste the following code:
'_______________________________________
Option Explicit
Private Const strStyle As String = "Num"

'_______________________________________
Sub ApplyNum()

Dim rgePara As Range

Set rgePara = Selection.Range.Paragraphs(1).Range

With rgePara
If .Style = "Num" Then
MsgBox "The current paragraph style is already """ _
& strStyle & """.", vbExclamation, "Cancelled"
Exit Sub
End If
.Collapse wdCollapseStart
.Style = strStyle
.InsertAfter strStyle
.InsertAutoText
.Fields(1).Result.Font.Bold = True
.Collapse wdCollapseEnd
.Select
End With

End Sub
'_______________________________________

'_______________________________________
Sub RemoveNum()

Dim rgePara As Range

Set rgePara = Selection.Range.Paragraphs(1).Range

With rgePara
If .Style <> strStyle Then
MsgBox "The current paragraph style is not """ _
& strStyle & """.", vbExclamation, "Cancelled"
Exit Sub
End If
.Fields(1).Result.Delete
.Characters(1).Delete
.Style = "Normal"
End With

End Sub
'_______________________________________
5) Close the VBA Editor Window
6) Tools > Customize > Commands tab > Keyboard... button
7) In the category on the left, select "Macros"
8) IN the list on the Right, select ApplyNum
9) Click inside the "Press shortcut key:" area
10) On the keyboard, do "CTRL-1"
11) Click on the "Assign" button (Of course, use whatever shortcut key
combination)
12) IN the list on the Right, select RemoveNum
13) Click inside the "Press shortcut key:" area
14) On the keyboard, do "SHIFT-CTRL-1"
15) Click on the "Assign" button (This is why I used CTRL-1, so that I can
now do SHIFT-CTRL-! to undo, but you could use CTRL-2 here. Of course, if
you already use CTRL-1 for its default shortcut, find another key
assignment)
16) Click on "Close" twice

Now all you have to do is CTRL-1 to apply the style, or SHIFT-CTRL-1 to
"un-apply" it.

If you don't like keyboard shortcuts, you can create toolbar buttons
instead.

Of course, if you have more than one document using this, you may want to
create a template and store all the customization in that template instead
of using Normal

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

Jean-Guy Marcil

J.I.B. was telling us:
J.I.B. nous racontait que :
That partially did what I needed. When I run the macro, it makes a
line number as needed. But when I am done typing the paragraph and
hit Enter, the next line does not have the number, but is still in
the correct style. Did I miss something?

No, the numbering is not part of the style, it is a field.

Since the macro will not run on a paragraph already styled with the style
used in the macro, set the Next Style of "Num" to be Normal when you define
the style.
Thus, when you will hit Enter, the Next paragraph will Be normal, and if
this new paragraph also needs a number, the macro will have to be run again.
There is no other way. The SEQ field has to be inserted manually, through an
AutoText or with a macro, it cannot be part of a style.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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