Checking whether paragraphs have been formatted with outline numberedheadings or manually?

A

andreas

Dear Experts:

I got a document where outline numbered built-in headings have been
used in most cases but not always, i.e. sometimes a couple of headings
have been numbered manually such as

2.3 Analysis (outline numbered with built-in heading 2)
2.3.1 In-Depth analysis (manually numbered)
2.3.2 General analysis (manually numbered)
3 Future Outlook (outline numbered with built-in heading 1)

I wonder whether a macro is able ...
.... to loop through all paragraphs starting with a number followed by
a tab stop or spaces followed by words/characters and ...
.... check whether these occurrences have been formatted with the built-
in numbered headings or manually?

In cases where manually formatting has been detected, these
occurrences have to be displayed in a msgbox.

I hope this is not too much to ask / exceeds the scope of this forum.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
L

Lene Fredborg

The macro below will most likely do most of what you want. However, you may
need to adjust the formatting of numbers to search for and/or add more
variations, e.g. with 2 digits. See the comments in the code. As you will
see, I have created the code so red highlight is applied to the paragraphs
with manually applied numbers. If you don’t want that, delete or comment out
the code line “Range.HighlightColorIndex = wdRedâ€.

Note that the code does not check the applied style since I imagine that the
manually numbered paragraphs could be formatted with any style (maybe most
likely Normal + direct formatting).


Sub FindAndMarkManuallyNumberedParagraphs()

Dim oPara As Paragraph
Dim strMsg As String
Dim oArray As Variant
Dim n As Long

Application.ScreenUpdating = False

'Create an array of strings to check
'Here only 5 levels in format 1 or 1., 1.1, 1.1.1, 1.1.1.1, 1.1.1.1.1
are included
'Also, the code will not find e.g. 12.11.1 - to do this, include more
variations in the array
oArray = Array("# ", "#" & vbTab, _
"#. ", "#." & vbTab, _
"#.# ", "#.#" & vbTab, _
"#.#.# ", "#.#.#" & vbTab, _
"#.#.#.# ", "#.#.#.#" & vbTab, _
"#.#.#.#.# ", "#.#.#.#.#" & vbTab)

For n = LBound(oArray) To UBound(oArray)
For Each oPara In ActiveDocument.Paragraphs
'Find paragraphs starting with numbers
'if the ListString is empty, the number is added manually
With oPara
If Left(.Range.Text, Len(oArray(n))) Like oArray(n) = True
Then
If .Range.ListFormat.ListString = "" Then
'Manually numbered – append number to msg - excl.
space or tab
strMsg = strMsg & Trim(Left(.Range.Text,
Len(oArray(n)))) & vbCr
'Mark oPara with red highlight
.Range.HighlightColorIndex = wdRed
End If
End If
End With
Next oPara
Next n

Application.ScreenUpdating = True

'Show final msg
If strMsg = "" Then
MsgBox "No manually applied numbers were found."
Else
MsgBox "The numbers of the following paragraphs have been manually
applied. The para-graphs have been marked with red highlight:" & vbCr & vbCr
& strMsg
End If

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

The macro below will most likely do most of what you want. However, you may
need to adjust the formatting of numbers to search for and/or add more
variations, e.g. with 2 digits. See the comments in the code. As you will
see, I have created the code so red highlight is applied to the paragraphs
with manually applied numbers. If you don’t want that, delete or comment out
the code line “Range.HighlightColorIndex = wdRed”.

Note that the code does not check the applied style since I imagine that the
manually numbered paragraphs could be formatted with any style (maybe most
likely Normal + direct formatting).

Sub FindAndMarkManuallyNumberedParagraphs()

    Dim oPara As Paragraph
    Dim strMsg As String
    Dim oArray As Variant
    Dim n As Long

    Application.ScreenUpdating = False

    'Create an array of strings to check
    'Here only 5 levels in format 1 or 1., 1.1, 1.1.1, 1.1.1.1, 1.1.1..1.1
are included
    'Also, the code will not find e.g. 12.11.1 - to do this, include more
variations in the array
    oArray = Array("# ", "#" & vbTab, _
        "#. ", "#." & vbTab, _
        "#.# ", "#.#" & vbTab, _
        "#.#.# ", "#.#.#" & vbTab, _
        "#.#.#.# ", "#.#.#.#" & vbTab, _
        "#.#.#.#.# ", "#.#.#.#.#" & vbTab)

    For n = LBound(oArray) To UBound(oArray)
        For Each oPara In ActiveDocument.Paragraphs
            'Find paragraphs starting with numbers
            'if the ListString is empty, the number is added manually
            With oPara
                If Left(.Range.Text, Len(oArray(n))) LikeoArray(n) = True
Then
                    If .Range.ListFormat.ListString = "" Then
                        'Manually numbered – append number to msg - excl.
space or tab
                        strMsg = strMsg & Trim(Left(.Range.Text,
Len(oArray(n)))) & vbCr
                        'Mark oPara with red highlight
                        .Range.HighlightColorIndex = wdRed
                    End If
                End If
            End With
        Next oPara
    Next n

    Application.ScreenUpdating = True

    'Show final msg
    If strMsg = "" Then
        MsgBox "No manually applied numbers were found."
    Else
        MsgBox "The numbers of the following paragraphs have beenmanually
applied. The para-graphs have been marked with red highlight:" & vbCr & vbCr
& strMsg
    End If

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word












- Show quoted text -

Dear Lene,

thank you so much for your professional help. This is professionalism
at its best. Terrific! It is working as desired. I adapted your code
slightly to include the whole text of the found ranges as well as the
page number of these occurrences.

If .range.ListFormat.ListString = "" Then
strMsg = strMsg & .range.Text & "Page "
& .range.Information(wdActiveEndAdjustedPageNumber) & vbCrLf

There is one small thing I would like to ask you. Your macro also
hilights inserted table of contents. Is it possible to re-write your
code so that the code only loops thru paragraphs starting from the
section with the first occurrence of built-in heading level 1. This
could be section 2 or 3 or any other section number (with the
exception of section 1).

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
L

Lene Fredborg

The reason why I only included the heading number in the message was that a
MsgBox only allows less than 1024 characters (but I did not explicitly tell
that…). If many and long headings were found, you could hit the max. limit so
that the lasts paragraphs would be missing in the message.

About skipping the TOC: This could be done in different ways. Below you will
find a new version of the macro where a paragraph will be skipped if it is in
the (first) table of contents in the document. If your document contains more
than one TOC, you would need to check for other TOCs too. You will need to
incorporate your own changes in the new macro version:


Sub FindAndMarkManuallyNumberedParagraphs()

Dim oPara As Paragraph
Dim strMsg As String
Dim oArray As Variant
Dim n As Long
Dim nToc As Long

Application.ScreenUpdating = False

'Find number of TOCs
nToc = ActiveDocument.TablesOfContents.Count

'Create an array of strings to check
'Here only 5 levels in format 1 or 1., 1.1, 1.1.1, 1.1.1.1, 1.1.1.1.1
are included
'Also, the code will not find e.g. 12.11.1 - to do this, include more
variations in the array
oArray = Array("# ", "#" & vbTab, _
"#. ", "#." & vbTab, _
"#.# ", "#.#" & vbTab, _
"#.#.# ", "#.#.#" & vbTab, _
"#.#.#.# ", "#.#.#.#" & vbTab, _
"#.#.#.#.# ", "#.#.#.#.#" & vbTab)

For n = LBound(oArray) To UBound(oArray)
For Each oPara In ActiveDocument.Paragraphs
'Find paragraphs starting with numbers
'Skip if in TOC
If nToc > 0 Then
If
oPara.Range.InRange(ActiveDocument.TablesOfContents(1).Range) = True Then
GoTo SkipPara
End If
'if the ListString is empty, the number is added manually
With oPara
If Left(.Range.Text, Len(oArray(n))) Like oArray(n) = True
Then
If .Range.ListFormat.ListString = "" Then
'Manually numbered - append number to msg - excl.
space or tab
strMsg = strMsg & Trim(Left(.Range.Text,
Len(oArray(n)))) & vbCr
'Mark oPara with red highlight
.Range.HighlightColorIndex = wdRed
End If
End If
End With
SkipPara:
Next oPara
Next n

Application.ScreenUpdating = True

'Show final msg
If strMsg = "" Then
MsgBox "No manually applied numbers were found."
Else
MsgBox "The numbers of the following paragraphs have been manually
applied. The para-graphs have been marked with red highlight:" & vbCr & vbCr
& strMsg
End If

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

The reason why I only included the heading number in the message was thata
MsgBox only allows less than 1024 characters (but I did not explicitly tell
that…). If many and long headings were found, you could hit the max. limit so
that the lasts paragraphs would be missing in the message.

About skipping the TOC: This could be done in different ways. Below you will
find a new version of the macro where a paragraph will be skipped if it is in
the (first) table of contents in the document. If your document contains more
than one TOC, you would need to check for other TOCs too. You will need to
incorporate your own changes in the new macro version:

Sub FindAndMarkManuallyNumberedParagraphs()

    Dim oPara As Paragraph
    Dim strMsg As String
    Dim oArray As Variant
    Dim n As Long
    Dim nToc As Long

    Application.ScreenUpdating = False

    'Find number of TOCs
    nToc = ActiveDocument.TablesOfContents.Count

    'Create an array of strings to check
    'Here only 5 levels in format 1 or 1., 1.1, 1.1.1, 1.1.1.1, 1.1.1..1.1
are included
    'Also, the code will not find e.g. 12.11.1 - to do this, include more
variations in the array
    oArray = Array("# ", "#" & vbTab, _
        "#. ", "#." & vbTab, _
        "#.# ", "#.#" & vbTab, _
        "#.#.# ", "#.#.#" & vbTab, _
        "#.#.#.# ", "#.#.#.#" & vbTab, _
        "#.#.#.#.# ", "#.#.#.#.#" & vbTab)

    For n = LBound(oArray) To UBound(oArray)
        For Each oPara In ActiveDocument.Paragraphs
            'Find paragraphs starting with numbers
            'Skip if in TOC
            If nToc > 0 Then
                If
oPara.Range.InRange(ActiveDocument.TablesOfContents(1).Range) = True Then
GoTo SkipPara
            End If
            'if the ListString is empty, the number is added manually
            With oPara
                If Left(.Range.Text, Len(oArray(n))) LikeoArray(n) = True
Then
                    If .Range.ListFormat.ListString = "" Then
                        'Manually numbered - append number to msg - excl.
space or tab
                        strMsg = strMsg & Trim(Left(.Range.Text,
Len(oArray(n)))) & vbCr
                        'Mark oPara with red highlight
                        .Range.HighlightColorIndex = wdRed
                    End If
                End If
            End With
SkipPara:
        Next oPara
    Next n

    Application.ScreenUpdating = True

    'Show final msg
    If strMsg = "" Then
        MsgBox "No manually applied numbers were found."
    Else
        MsgBox "The numbers of the following paragraphs have beenmanually
applied. The para-graphs have been marked with red highlight:" & vbCr & vbCr
& strMsg
    End If

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word










- Zitierten Text anzeigen -

Dear Lene,

thank you very much for your quick help. I am getting an error message
on line

SkipPara:
Next oPara
.....

Next oPara gets highlighted and it says: Next without for.

It would be fantastic if you could clear this error. Thank you very
much in advance. Regards, Andreas
 
L

Lene Fredborg

I tested the macro without any error before posting it and it worked - and it
still does. I now tried to copy the macro from the Internet and I found that
your problem could be due to an erroneous line break that appeared when you
copied the macro from the Internet.

Check this part of the macro:

If oPara.Range.InRange(ActiveDocument.TablesOfContents(1).Range) = True Then
GoTo SkipPara

Is it all on _one_ line? (Goto SkipPara may have moved to next line and this
is not marked by red in the editor). Make sure to combine the string in one
line. Alternatively, add an extra “End If†below it.

Does this solve the problem?

Note: When posting the code, I forgot to remove the “Dim i as long†which is
not used. You can delete it if you wish.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
A

andreas

I tested the macro without any error before posting it and it worked - and it
still does. I now tried to copy the macro from the Internet and I found that
your problem could be due to an erroneous line break that appeared when you
copied the macro from the Internet.

Check this part of the macro:

If oPara.Range.InRange(ActiveDocument.TablesOfContents(1).Range) = TrueThen
GoTo SkipPara

Is it all on _one_ line? (Goto SkipPara may have moved to next line and this
is not marked by red in the editor). Make sure to combine the string in one
line. Alternatively, add an extra “End If” below it.

Does this solve the problem?

Note: When posting the code, I forgot to remove the “Dim i as long” which is
not used. You can delete it if you wish.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word










- Zitierten Text anzeigen -


Lene,

ok. Thank you very much. I'll give it a try tomorrow at the latest and
let you know.

Kind regards, Andreas
 
A

andreas

I tested the macro without any error before posting it and it worked - and it
still does. I now tried to copy the macro from the Internet and I found that
your problem could be due to an erroneous line break that appeared when you
copied the macro from the Internet.

Check this part of the macro:

If oPara.Range.InRange(ActiveDocument.TablesOfContents(1).Range) = TrueThen
GoTo SkipPara

Is it all on _one_ line? (Goto SkipPara may have moved to next line and this
is not marked by red in the editor). Make sure to combine the string in one
line. Alternatively, add an extra “End If” below it.

Does this solve the problem?

Note: When posting the code, I forgot to remove the “Dim i as long” which is
not used. You can delete it if you wish.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmarkwww.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word










- Show quoted text -

Hey Lene,

thank you for the tip. This was exactly the case. Again, thank you so
much for your professional help. I really appreciate it.

Kind regards, Andreas
 
L

Lene Fredborg

You are welcome. I am glad I could help.

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 

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