PDA

View Full Version : [SOLVED:] Sequentially Insert Numbers Before all Headings 1



saphire99
12-25-2015, 05:59 AM
Hello,

to all - hope everyone is having a great day today! :hi:

I have a numbering problem that I have been trying to work out for a while.

I want to Insert sequential numbers in front of my heading 1

For example


1. Science

2. Maths

3. History

I have inserted an autonum field - but it is not working when I run it on other heading 1.



Sub InsertNumber()

'Insert Sequential Numbers at the beginning of Heading 1


With ActiveDocument.Styles("Heading 1").Font

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTONUM \* Arabic ", PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTONUM \* Arabic ", PreserveFormatting:=False
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"AUTONUM \* Arabic ", PreserveFormatting:=False
End Sub



I am a bit lost.

I wouldn't imagine it to be this troublesome to insert some numbers before the headings but alas it is.
I have also researched online but nothing has come up.

Please advise - I really appreciate the help

thank you so much :grinhalo:

Saphire

gmaxey
12-25-2015, 06:30 AM
Here is a macro solution:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPar As Paragraph
Dim oRng As Word.Range
For Each oPar In ActiveDocument.Range.Paragraphs
If oPar.Style = "Heading 1" Then
Set oRng = oPar.Range
oRng.InsertBefore ". "
oRng.Collapse wdCollapseStart
oRng.Fields.Add oRng, wdFieldSequence, "Nums"
End If
Next
lbl_Exit:
Exit Sub
End Sub


However, I would select one of the Heading 1 paragraphs and the use the Home>Paragraph>Multilevel List control to set a predefined or create a custom numbering style.

saphire99
12-25-2015, 07:03 AM
Hello Greg,

and happy seasonal greetings!

This is phenomenal.

I was :banghead: for months.

I had a large document with hundreds of headings, as i didn't know how to do this.

I had a very long complex process based on a different macro example

I inserted a placeholder at the end of the heading 1
Moved it to the front of heading 1
replaced with autoNum

I know it doesn't make sense I gave up in the end.

I was trying to sequentially number headings and - there just is never enough good information - or MR GOOGLE refuses to serve you the right information - so often times you have to try and patch things with the tools you have, adapting macros not intended for the job.

This is awesomely delicious - a very useful and helpful vba module!

thank you so much

Saphire


Have a great rest of day / eve :biggrin:

:beerchug:


Please Mark as Solved