Word Macro

C

Casey

I need to make a mocro that will work in a word doc. This macro would
start with [000001] and count up one number each paragraph. For example
paragraph two would auto show [000002] and so on. Any help would be
GREAT!
 
C

Cindy M -WordMVP-

Hi Casey,
I need to make a mocro that will work in a word doc. This macro would
start with [000001] and count up one number each paragraph. For example
paragraph two would auto show [000002] and so on. Any help would be
GREAT!
Why a macro? Why not just use Word's AutoNumbering, formatted to look the
way you want, and apply it to all the paragraphs?

If you need help customizing number formatting, please tell us which
version of Word you have.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
T

Tony Jollans

Hi Cindy,

I agree that numbering would would be better than code but ...

How do you format numbering to have leading zeroes like that?

--
Enjoy,
Tony


Cindy M -WordMVP- said:
Hi Casey,
I need to make a mocro that will work in a word doc. This macro would
start with [000001] and count up one number each paragraph. For example
paragraph two would auto show [000002] and so on. Any help would be
GREAT!
Why a macro? Why not just use Word's AutoNumbering, formatted to look the
way you want, and apply it to all the paragraphs?

If you need help customizing number formatting, please tell us which
version of Word you have.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 
C

Cindy M -WordMVP-

Hi Tony,
I agree that numbering would would be better than code but ...

How do you format numbering to have leading zeroes like that?
Mmmm, in the light of day, I see what you're getting at...

In that case, I'd probably be inclined to go with SEQ fields so
that updating the document would be easier. Just add fields
where none currently are present. Casey, how about this for
starters:

Sub NumberAllParas()
Dim para As Word.Paragraph
Dim doc As Word.Document
Dim rng As Word.Range
Dim numFormat As String
Dim fieldcode As String

numFormat = "[00000] "
fieldcode = "SEQ paras \# " & numFormat
Set doc = ActiveDocument
For Each para In doc.Paragraphs
Set rng = para.Range
rng.TextRetrievalMode.IncludeFieldCodes = True
If rng.Words(1).Fields.Count = 0 Then
InsertSEQField rng, fieldcode
'Check that it's not an SEQ field from this sequence
ElseIf rng.Words(1).Fields(1).Code <> _
" " & fieldcode & " " Then
InsertSEQField rng, fieldcode
End If
Next
End Sub

Private Sub InsertSEQField(rng As Word.Range, _
fieldText As String)
rng.Collapse wdCollapseStart
'In case the range is invalid for inserting a field
On Error Resume Next
rng.Fields.Add Range:=rng, _
Text:=fieldText, _
PreserveFormatting:=False
On Error GoTo 0
End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8
2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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