J
Jessica Weissman
I may have bitten off more than I can chew, but I am working on a macro that
counts instances of styles in the entire document, including headers and
footers as well as the main body of the document. This is Word 2003.
I run through each paragraph of each storyrange and grab its style, place it
in a Dictionary if it isn't already there and up the count. This ought to
get me a list of styles with a count for each one.
Several questions:
1) Is there an automated way to have this skip the footnote and endnote
related stories other than the brute force thing I used?
2) Is there any way to get it to skip footers and headers that are there but
not visible, such as the left and right page footers for sections that don't
have any such? Word seems to create them and hide them for sections that are
one page long.
3) Why does it find jillions of instances of Normal style that are not
visible? I have a document that claims to have 39 instances of Normal style
in the main text story. But when I use the Word interface to search for the
text in Normal style in the main text, it doesn't find any.
Many thanks for any insights. I'm probably doing this the hard way.
- jessica
Here is the messy code I am using:
Sub FindAllUsedStyles2()
Dim sAllStyles() As String
Dim vStyles As Variant
Dim i As Long
Dim idx As Long
Dim cParas As Long
Dim cStyles As Long
Dim cUsedStyles As Long
Dim sPrevStyle As String
Dim para As Paragraph
Dim aStory As Word.Range
Dim styleDict As New Scripting.Dictionary
Dim aStyle As Style
Dim aStyleName As String
Dim aKey As Long
Dim tempKey As Long
Dim myRange As Range
' put styles from the entire doc including headers and footers into a
dictionary and count occurrences
For Each aStory In ActiveDocument.StoryRanges
getstorytype = Choose(aStory.StoryType, "wdMainTextStory",
"wdFootnotesStory", "wdEndnotesStory", "wdCommentsStory", "wdTextFrameStory",
"wdEvenPagesHeaderStory", "wdPrimaryHeaderStory", "wdEvenPagesFooterStory",
"wdPrimaryFooterStory", "wdFirstPageHeaderStory", "wdFirstPageFooterStory",
"wdFootnoteSeparatorStory", "wdFootnoteContinuationSeparatorStory",
"wdFootnoteContinuationNoticeStory", "wdEndnoteSeparatorStory",
"wdEndnoteContinuationSeparatorStory", "wdEndnoteContinuationNoticeStory")
If ((Left(getstorytype, 9) <> "wdEndnote") And (Left(getstorytype, 10)
<> "wdFootnote")) Then
For Each para In aStory.Paragraphs
Set aStyle = para.Style
aStyleName = aStyle.NameLocal
If styleDict.Exists(aStyleName) Then
tempKey = (styleDict.Item(aStyleName)) + 1
styleDict.Item(aStyleName) = tempKey
Else
styleDict.Add aStyleName, 1
End If
Next para
End If
Do Until aStory.NextStoryRange Is Nothing
Set aStory = aStory.NextStoryRange
For Each para In aStory.Paragraphs
Set aStyle = para.Style
aStyleName = aStyle.NameLocal
If styleDict.Exists(aStyleName) Then
tempKey = (styleDict.Item(aStyleName)) + 1
styleDict.Item(aStyleName) = tempKey
Else
styleDict.Add aStyleName, 1
End If
Next para
Loop
Next aStory
' Count of styles in the styles dictionary
cStyles = styleDict.Count
vStyles = styleDict.Keys
' Create a new document and insert the style names
Documents.Add
For i = 0 To cStyles - 1
Selection.InsertAfter i & " " & vStyles(i) & " " &
styleDict.Item(vStyles(i)) & vbCr
Selection.Collapse wdCollapseEnd
Next i
End Sub
counts instances of styles in the entire document, including headers and
footers as well as the main body of the document. This is Word 2003.
I run through each paragraph of each storyrange and grab its style, place it
in a Dictionary if it isn't already there and up the count. This ought to
get me a list of styles with a count for each one.
Several questions:
1) Is there an automated way to have this skip the footnote and endnote
related stories other than the brute force thing I used?
2) Is there any way to get it to skip footers and headers that are there but
not visible, such as the left and right page footers for sections that don't
have any such? Word seems to create them and hide them for sections that are
one page long.
3) Why does it find jillions of instances of Normal style that are not
visible? I have a document that claims to have 39 instances of Normal style
in the main text story. But when I use the Word interface to search for the
text in Normal style in the main text, it doesn't find any.
Many thanks for any insights. I'm probably doing this the hard way.
- jessica
Here is the messy code I am using:
Sub FindAllUsedStyles2()
Dim sAllStyles() As String
Dim vStyles As Variant
Dim i As Long
Dim idx As Long
Dim cParas As Long
Dim cStyles As Long
Dim cUsedStyles As Long
Dim sPrevStyle As String
Dim para As Paragraph
Dim aStory As Word.Range
Dim styleDict As New Scripting.Dictionary
Dim aStyle As Style
Dim aStyleName As String
Dim aKey As Long
Dim tempKey As Long
Dim myRange As Range
' put styles from the entire doc including headers and footers into a
dictionary and count occurrences
For Each aStory In ActiveDocument.StoryRanges
getstorytype = Choose(aStory.StoryType, "wdMainTextStory",
"wdFootnotesStory", "wdEndnotesStory", "wdCommentsStory", "wdTextFrameStory",
"wdEvenPagesHeaderStory", "wdPrimaryHeaderStory", "wdEvenPagesFooterStory",
"wdPrimaryFooterStory", "wdFirstPageHeaderStory", "wdFirstPageFooterStory",
"wdFootnoteSeparatorStory", "wdFootnoteContinuationSeparatorStory",
"wdFootnoteContinuationNoticeStory", "wdEndnoteSeparatorStory",
"wdEndnoteContinuationSeparatorStory", "wdEndnoteContinuationNoticeStory")
If ((Left(getstorytype, 9) <> "wdEndnote") And (Left(getstorytype, 10)
<> "wdFootnote")) Then
For Each para In aStory.Paragraphs
Set aStyle = para.Style
aStyleName = aStyle.NameLocal
If styleDict.Exists(aStyleName) Then
tempKey = (styleDict.Item(aStyleName)) + 1
styleDict.Item(aStyleName) = tempKey
Else
styleDict.Add aStyleName, 1
End If
Next para
End If
Do Until aStory.NextStoryRange Is Nothing
Set aStory = aStory.NextStoryRange
For Each para In aStory.Paragraphs
Set aStyle = para.Style
aStyleName = aStyle.NameLocal
If styleDict.Exists(aStyleName) Then
tempKey = (styleDict.Item(aStyleName)) + 1
styleDict.Item(aStyleName) = tempKey
Else
styleDict.Add aStyleName, 1
End If
Next para
Loop
Next aStory
' Count of styles in the styles dictionary
cStyles = styleDict.Count
vStyles = styleDict.Keys
' Create a new document and insert the style names
Documents.Add
For i = 0 To cStyles - 1
Selection.InsertAfter i & " " & vStyles(i) & " " &
styleDict.Item(vStyles(i)) & vbCr
Selection.Collapse wdCollapseEnd
Next i
End Sub