Results 1 to 12 of 12
  1. #1
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Is there a way to automatically apply styles to headings based on their numbering?

    I didn't think this would be challenging but I've been unable to come up with a solution or find one online. When I convert a document from PDF to Word it is a very manual process applying styles to the different heading levels especially when dealing with a technical document with hundreds of headings.

    (heading 1 for #, heading 2 for #.#, heading 3 for #.#.# for example).

    Is there a way to do this automatically? I tried to create a macro but with limited success.

    Any assistance is greatly appreciated.

    thx!

  2. #2
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try the following macro:
    Code:
    Sub AutoNumber()
    Dim Para As Paragraph, Rng As Range, iLvl As Long
    With ActiveDocument.Range
      For Each Para In .Paragraphs
        Set Rng = Para.Range.Words.First
        With Rng
          If IsNumeric(.Text) Then
            While .Characters.Last.Next.Text Like "[0-9. " & vbTab & "]"
              .End = .End + 1
            Wend
            iLvl = UBound(Split(.Text, "."))
            .Text = vbNullString
            Para.Style = "Heading " & iLvl
          End If
        End With
      Next
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  3. The Following User Says Thank You to macropod For This Useful Post:

    ogelthorp28 (2014-04-12)

  4. #3
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Hi Paul,
    Thanks so much for the prompt reply. When I run the macro I get a run time error '5834': "Item specified does not exist" referring to line:
    Para.Style = "Heading " & iLvl

    I played around with it and couldn`t figure out how to work around it. Ideas..

    Thanks again
    Mike

  5. #4
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts
    Word has 9 built-in heading levels. That is it. Does your number exceed 9?
    Charles Kyle Kenyon
    Madison, Wisconsin

  6. The Following User Says Thank You to Charles Kenyon For This Useful Post:

    ogelthorp28 (2014-04-12)

  7. #5
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thanks Charles,
    Good thought but nope, this may apply in some of the documents that I would like to automatically apply heading styles but not this one, but 5 levels would be enough. The numbering can exceed 9 for each of the levels, for example - 1.15.2.20. I took an extract of my document that is very simple, only a few pages with 3 heading levels and none in double digits with all normal text and I still receive the same error that I have described above.

    Br.
    Mike

  8. #6
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try the following modified version. The previous version assumed the numbers would be followed by periods, which evidently isn't always the case:
    Code:
    Sub AutoNumber()
    Dim Para As Paragraph, Rng As Range, iLvl As Long
    With ActiveDocument.Range
      For Each Para In .Paragraphs
        Set Rng = Para.Range.Words.First
        With Rng
          If IsNumeric(.Text) Then
            While .Characters.Last.Next.Text Like "[0-9. " & vbTab & "]"
              .End = .End + 1
            Wend
            iLvl = UBound(Split(.Text, "."))
            If IsNumeric(Split(.Text, ".")(UBound(Split(.Text, ".")))) Then iLvl = iLvl + 1
            If iLvl < 10 Then
              .Text = vbNullString
              Para.Style = "Heading " & iLvl
            End If
          End If
        End With
      Next
    End With
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  9. The Following User Says Thank You to macropod For This Useful Post:

    ogelthorp28 (2014-04-12)

  10. #7
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Fantastic, thanks Paul, I think it`s almost there. Your macro ran successfully to completion in my small test document as well as in another 50 page test document but came across the same run-time error as before but not immediately this time, it ran until I hit the appendices about 800 pages in. I`m still testing to try to figure out why.

    There were a couple small issues, I`m not sure how easy it would be to fix but the macro assigns the heading styles nicely except that it removes the numbering. I need to maintain the same numbering as in the original documents. Also, it turns the text under the headings into the same style text as the heading. I`m not sure whether it`s an easy fix to keep that text as normal...

    There`s some complexity to this!

    Thanks again for your help!!
    Br.
    Mike

  11. #8
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts
    Quote Originally Posted by ogelthorp28 View Post
    Fantastic, thanks Paul, I think it`s almost there. Your macro ran successfully to completion in my small test document as well as in another 50 page test document but came across the same run-time error as before but not immediately this time, it ran until I hit the appendices about 800 pages in. I`m still testing to try to figure out why.

    There were a couple small issues, I`m not sure how easy it would be to fix but the macro assigns the heading styles nicely except that it removes the numbering. I need to maintain the same numbering as in the original documents. Also, it turns the text under the headings into the same style text as the heading. I`m not sure whether it`s an easy fix to keep that text as normal...

    There`s some complexity to this!

    Thanks again for your help!!
    Br.
    Mike
    If the numbering is automatic numbering, it needs to be applied through the styles. You could unlink the numbering before running the macro. (I don't know how to do this but believe it can be done.)

    The issue of text following the heading also being converted, I suspect is due to there not really being a separate style for the two, just different direct formatting. If so, you will need to actually apply a body text style to the body text.
    Charles Kyle Kenyon
    Madison, Wisconsin

  12. #9
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Quote Originally Posted by ogelthorp28 View Post
    Fantastic, thanks Paul, I think it`s almost there. Your macro ran successfully to completion in my small test document as well as in another 50 page test document but came across the same run-time error as before but not immediately this time, it ran until I hit the appendices about 800 pages in. I`m still testing to try to figure out why.
    If you examine the first numbered paragraph after the last one to which the macro applied a heading Style, whatever's different about that one to whatever the preceding updated paragraphs should give a clue.
    the macro assigns the heading styles nicely except that it removes the numbering. I need to maintain the same numbering as in the original documents.
    It's not so much that the macro is removing the numbers, true though that may be, but that your Heading Styles lack them. By default Word's heading Styles include multi-level numbering. That's why the macro removes them - so you don't end up with the numbers that go with the Styles, plus the hard numbering. Since you asked for the macro to apply heading Styles, that was the most appropriate course to follow. If you restore the heading Style numbering, either by re-linking the multi-level list numbering to the heading Styles, or by using the Organizer to re-insert the Heading Styles from template that has them, the numbering will be fixed.
    Also, it turns the text under the headings into the same style text as the heading. I`m not sure whether it`s an easy fix to keep that text as normal...
    I don't know what else you'd expect the application of Heading Styles - which you asked for - to do. The simple solution is to modify the Styles so they're formatted as you require.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  13. #10
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    I don't know what else you'd expect the application of Heading Styles - which you asked for - to do. The simple solution is to modify the Styles so they're formatted as you require.
    Hi Paul, yes I understand that the heading styles can be modified to include numbering but this could potentially cause the section numbers to change. This poses a huge problem since I'm working with contract requirements, the numbering from the document has to remain the same as the original even if there is an inconsistency or mistake in the sequence. If I apply the same heading styles manually, the numbering is not removed so I thought this could be possible.

    Best regards
    Mike

  14. #11
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    In that case you could delete '.Text = vbNullString' and the original manual numbering will be retained.

    For future reference, though, you need to be aware that Heading Styles normally include Word's auto numbering, just as they include specific bold/italic fonts & indents. The only reason you got the fonts but not the numbers is that someone has already modified the numbering aspect of those Styles.
    Cheers,

    Paul Edstein
    [MS MVP - Word]

  15. The Following User Says Thank You to macropod For This Useful Post:

    ogelthorp28 (2014-04-13)

  16. #12
    New Lounger
    Join Date
    Apr 2014
    Posts
    6
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by macropod View Post
    In that case you could delete '.Text = vbNullString' and the original manual numbering will be retained.

    For future reference, though, you need to be aware that Heading Styles normally include Word's auto numbering, just as they include specific bold/italic fonts & indents. The only reason you got the fonts but not the numbers is that someone has already modified the numbering aspect of those Styles.
    Thanks Paul, this code will help reduce hours of manual work. There`s still some checking and clean-up required as if there are numbers in the body of the document, they are sometimes mistaken as headings. But this macro will reduce a week`s effort to under a day.

    Thanks again,
    Mike

Posting Permissions

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