Consulting

Results 1 to 3 of 3

Thread: Sequentially Insert Numbers Before all Headings 1

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Location
    UK
    Posts
    31

    Sequentially Insert Numbers Before all Headings 1

    Hello,

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

    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

    Saphire

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    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.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Dec 2015
    Location
    UK
    Posts
    31

    Smile

    Hello Greg,

    and happy seasonal greetings!

    This is phenomenal.

    I was 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




    Please Mark as Solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •