Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2003 - paragraph style auditing 1

Status
Not open for further replies.

Carthesis

Technical User
Oct 29, 2002
100
GB
Hello.

I'm trying to set up a document template for generating reports. I've been through and created the layout, created all the styles etc. etc., and have been trying to set up some VBA which will reset all the styles I have defined to preset values, in case someone fiddles with everything where they shouldn't.

I have created (admittedly using the Macro recorder to save myself typing) subroutines that contain the format settings for each of the styles in use (Section Heading, Paragraph Heading etc.), however annoyingly if the formatting of the document is changed, running the appropriate subroutine only resets the style if the particular amended text is highlighted.

For example (and for clarity):
If I change the tab settings on the style 'Section Headings' from a single tab at 2.5cm to a single tab at 6cm, and then run the appropriate settings macro, it only resets the tabs on the altered line if either the cursor is somewhere IN that line, or if that line is highlighted.

My question therefore is two-fold:

a) how do I convince Word/VBA to change these things back even if the cursor is NOT in the line of text? (I suspect this may be impossible as Word is creating 'hidden' intermediate styles, hence the style name for these styles won't match the pre-defined ones in the code (ie. the style name would be 'Section Heading + 14pt. Arial' instead of 'Section Heading')

b) Is there a way to select all instances of a particular style in VBA (ie. like using the 'Select all instances of this style' menu option from the Styles and Formatting toolbox) so that I can just cycle through each style name in code and select all instances of it before running the reset code?

I can provide a sample document with the code I have so far if needed.

Many thanks in advance for the torrent of help I'm potentially about to receive!

 
Sounds like your code is applying formatting to range objects, not working with the styles themselves.
 



You might try...
Code:
Selection.Range.Paragraph.Style = MyStyle


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 


oops...
Code:
Selection.Range.Paragraphs(1).Style = MyStyle


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Skip:

I'll try that tomorrow when I find some more free time to work on it, thanks.

mintjulep:

Code I'm using for the formatting is below. I'll admit, I was lazy and got it from the Macro Recorder because I couldn't be bother to type too much :)

Code:
Sub Section_Heading()
'
' Paragraph formatting information for paragraph style '1. Section Heading'
'
    With ActiveDocument.Styles("Section heading").Font
        .Name = "Arial Bold"
        .Size = 14
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
    End With

    With ActiveDocument.Styles("Section heading").ParagraphFormat
        .LeftIndent = CentimetersToPoints(2.5)
        .RightIndent = CentimetersToPoints(1)
        .SpaceBefore = 12
        .SpaceBeforeAuto = False
        .SpaceAfter = 18
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceAtLeast
        .LineSpacing = 16
        .Alignment = wdAlignParagraphJustify
        .WidowControl = True
        .KeepWithNext = True
        .KeepTogether = True
        .PageBreakBefore = True
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = CentimetersToPoints(-1.5)
        .OutlineLevel = wdOutlineLevel1
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
    End With

    ActiveDocument.Styles("Section heading"). _
            NoSpaceBetweenParagraphsOfSameStyle = False
    ActiveDocument.Styles("Section heading").ParagraphFormat.TabStops.ClearAll
    ActiveDocument.Styles("Section heading").ParagraphFormat.TabStops.Add _
            Position:=CentimetersToPoints(2.5), Alignment:=wdAlignTabLeft, Leader:= _
            wdTabLeaderSpaces

    With ActiveDocument.Styles("Section heading")
        .AutomaticallyUpdate = False
        .BaseStyle = ""
        .NextParagraphStyle = "Paragraph Heading"
    End With

End Sub
 
Looks basically similar to something I have to do the same thing.

One thing that I have that you seem to be lacking is

.LinkedStyle = "Heading 1, Chapter #"

I'm thinking that Word has the styles linked, and is applying the "higher level" or paragraph formatting preferentially to your lower lever style format.

Styles are a wonderfully powerful feature, and an absolute pain to really use properly.

To get everything to work properly you need to get all your styles tied together and defined systematically so that changes propagate through to all levels properly. You might have a collection of unlinked styles rather than a system of linked styles.

For your reference, the following works for me:

Code:
Sub FixHeadings()
'

'
    With ActiveDocument.Styles("Heading 1,Chapter #").ParagraphFormat
        .LeftIndent = InchesToPoints(0.25)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 12
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(-0.25)
        .OutlineLevel = wdOutlineLevel1
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .AutoAdjustRightIndent = True
        .DisableLineHeightGrid = False
        .FarEastLineBreakControl = True
        .WordWrap = True
        .HangingPunctuation = True
        .HalfWidthPunctuationOnTopOfLine = False
        .AddSpaceBetweenFarEastAndAlpha = True
        .AddSpaceBetweenFarEastAndDigit = True
        .BaseLineAlignment = wdBaselineAlignAuto
    End With
    ActiveDocument.Styles("Heading 1,Chapter #"). _
        NoSpaceBetweenParagraphsOfSameStyle = False
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(1)
        .NumberFormat = "%1"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.25)
        .TabPosition = InchesToPoints(0.25)
        .ResetOnHigher = 0
        .StartAt = CInt(InputBox("Section"))
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 1, Chapter #"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(2)
        .NumberFormat = "%1.%2"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.1)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.5)
        .TabPosition = InchesToPoints(0.5)
        .ResetOnHigher = 1
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 2,1 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(3)
        .NumberFormat = "%1.%2.%3"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.2)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.75)
        .TabPosition = InchesToPoints(0.75)
        .ResetOnHigher = 2
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 3,2 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(4)
        .NumberFormat = "%1.%2.%3.%4"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.3)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1)
        .TabPosition = InchesToPoints(1)
        .ResetOnHigher = 3
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 4,3 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(5)
        .NumberFormat = "%1.%2.%3.%4.%5"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.4)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.25)
        .TabPosition = InchesToPoints(1.25)
        .ResetOnHigher = 4
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 5,4 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(6)
        .NumberFormat = "%1.%2.%3.%4.%5.%6"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.5)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.5)
        .TabPosition = InchesToPoints(1.5)
        .ResetOnHigher = 5
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 6,5 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(7)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.6)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.75)
        .TabPosition = InchesToPoints(1.75)
        .ResetOnHigher = 6
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 7,6 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(8)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.7)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(2)
        .TabPosition = InchesToPoints(2)
        .ResetOnHigher = 7
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 8,7 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(9)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8.%9"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(2.25)
        .TabPosition = InchesToPoints(2.25)
        .ResetOnHigher = 8
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 9,8 Dot Heading"
    End With
    ActiveDocument.Styles("Heading 1").LinkToListTemplate ListTemplate:= _
        ListGalleries(wdOutlineNumberGallery).ListTemplates(4), ListLevelNumber:= _
        1
    With ActiveDocument.Styles("Heading 1")
        .AutomaticallyUpdate = False
        .BaseStyle = ""
        .NextParagraphStyle = "1 Dot Text"
    End With
End Sub
Sub FixText()
'

'
    With ActiveDocument.Styles("1 Dot Text").ParagraphFormat
        .LeftIndent = InchesToPoints(0.5)
        .RightIndent = InchesToPoints(0)
        .SpaceBefore = 0
        .SpaceBeforeAuto = False
        .SpaceAfter = 12
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceSingle
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = False
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = InchesToPoints(-0.4)
        .OutlineLevel = wdOutlineLevel2
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .AutoAdjustRightIndent = True
        .DisableLineHeightGrid = False
        .FarEastLineBreakControl = True
        .WordWrap = True
        .HangingPunctuation = True
        .HalfWidthPunctuationOnTopOfLine = False
        .AddSpaceBetweenFarEastAndAlpha = True
        .AddSpaceBetweenFarEastAndDigit = True
        .BaseLineAlignment = wdBaselineAlignAuto
    End With
    ActiveDocument.Styles("1 Dot Text").NoSpaceBetweenParagraphsOfSameStyle = _
        False
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(1)
        .NumberFormat = "%1"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.25)
        .TabPosition = InchesToPoints(0.25)
        .ResetOnHigher = 0
        .StartAt = 0
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 1, Chapter #"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(2)
        .NumberFormat = "%1.%2"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.1)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.5)
        .TabPosition = InchesToPoints(0.5)
        .ResetOnHigher = 1
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 2,1 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(3)
        .NumberFormat = "%1.%2.%3"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.2)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(0.75)
        .TabPosition = InchesToPoints(0.75)
        .ResetOnHigher = 2
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 3,2 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(4)
        .NumberFormat = "%1.%2.%3.%4"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.3)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1)
        .TabPosition = InchesToPoints(1)
        .ResetOnHigher = 3
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 4,3 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(5)
        .NumberFormat = "%1.%2.%3.%4.%5"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.4)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.25)
        .TabPosition = InchesToPoints(1.25)
        .ResetOnHigher = 4
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 5,4 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(6)
        .NumberFormat = "%1.%2.%3.%4.%5.%6"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.5)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.5)
        .TabPosition = InchesToPoints(1.5)
        .ResetOnHigher = 5
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 6,5 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(7)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.6)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(1.75)
        .TabPosition = InchesToPoints(1.75)
        .ResetOnHigher = 6
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 7,6 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(8)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.7)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(2)
        .TabPosition = InchesToPoints(2)
        .ResetOnHigher = 7
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 8,7 Dot Heading"
    End With
    With ListGalleries(wdOutlineNumberGallery).ListTemplates(4).ListLevels(9)
        .NumberFormat = "%1.%2.%3.%4.%5.%6.%7.%8.%9"
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleArabic
        .NumberPosition = InchesToPoints(0.8)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = InchesToPoints(2.25)
        .TabPosition = InchesToPoints(2.25)
        .ResetOnHigher = 8
        .StartAt = 1
        With .Font
            .Bold = wdUndefined
            .Italic = wdUndefined
            .StrikeThrough = wdUndefined
            .Subscript = wdUndefined
            .Superscript = wdUndefined
            .Shadow = wdUndefined
            .Outline = wdUndefined
            .Emboss = wdUndefined
            .Engrave = wdUndefined
            .AllCaps = wdUndefined
            .Hidden = wdUndefined
            .Underline = wdUndefined
            .Color = wdUndefined
            .Size = wdUndefined
            .Animation = wdUndefined
            .DoubleStrikeThrough = wdUndefined
            .Name = ""
        End With
        .LinkedStyle = "Heading 9,8 Dot Heading"
    End With
    ActiveDocument.Styles("1 Dot Text").LinkToListTemplate ListTemplate:= _
        ListGalleries(wdOutlineNumberGallery).ListTemplates(4), ListLevelNumber:= _
        2
    With ActiveDocument.Styles("1 Dot Text")
        .AutomaticallyUpdate = False
        .BaseStyle = "Heading 2"
        .NextParagraphStyle = "1 Dot Text"
    End With
End Sub

Sub FixTOC()
'
' Macro3 Macro
' Macro recorded 12/1/2009 by Robert May
'
    With ActiveDocument.Styles("TOC 1")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 1").Font
        
        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 12
        .Bold = True
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = True
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
    With ActiveDocument.Styles("TOC 2")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 2").Font
       
        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
    With ActiveDocument.Styles("TOC 3")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 3").Font

        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
        
            With ActiveDocument.Styles("TOC 4")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 4").Font

        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
        
        
            With ActiveDocument.Styles("TOC 5")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 5").Font

        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
    
        With ActiveDocument.Styles("TOC 6")
        .AutomaticallyUpdate = True
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
    With ActiveDocument.Styles("TOC 6").Font

        .NameAscii = "Arial"
        .NameOther = "Arial"
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
        .DisableCharacterSpaceGrid = False
        .EmphasisMark = wdEmphasisMarkNone
    End With
        
    End With
    With ActiveDocument
        '.TablesOfContents(1).Delete
        .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
            True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
            LowerHeadingLevel:=1, IncludePageNumbers:=True, AddedStyles:= _
            "Heading 2,2,Heading 3,3,Heading 4,4,Heading 5,5,Heading 6,6", _
            UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
            False
        .TablesOfContents(1).TabLeader = wdTabLeaderDots
        .TablesOfContents.Format = wdIndexIndent
    End With
End Sub
 
Interesting. I'll look into it, although I'm not sure you're right, because it IS applying exactly the right formatting in the right place, but only when the cursor is on that line.

Just not sure why the cursor needs to be on the line to do it is all.

And I've deliberately set up all the styles so that they aren't based off of each other. The only linkage between them is how you use them.
 
I guess it's boiling down to this line:
Code:
    With ActiveDocument.Styles("Section heading")
        [b].AutomaticallyUpdate = False[/b]
        .BaseStyle = ""
        .NextParagraphStyle = "Paragraph Heading"
    End With

With "Automatically Update" set to false, Word will not reset ALL instances of this style, only the selected one. That is why it will only reset the line the cursor is in.

So if you change this line to
Code:
[b].AutomaticallyUpdate = True[/b]
you should be all set.

Good luck!
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Hmm. I thought the Automatic update was supposed to filter through to update the styles based on that style...

I'll check when I get to work tomorrow and let you know...
 
I'm not sure that .AutomaticallyUpdate = False is the problem.

I seem to have that in my code, and I don't experience the problems that Carthesis is experiencing.
 
Skip:

Tried your suggestion of
Code:
Selection.Range.Paragraphs(1).Style = MyStyle
and I'm sorry to say it didn't work. It didn't select all instances of the specified style, and actually changed the text style of the preceding paragraph to the style specified in 'MyStyle'

MakeItSo:

Tried your suggestion of changing the AutomaticallyUpdate property to True, and that didn't work either.

Thanks for the assistance guys, I really appreciate it. But I'm forced now to ask politely if anyone has any other ideas?
 
Actually, I've had another look around, and I've sorted a way of doing it.

Posting it below in case anyone else has a similar situation they want to overcome.
Code:
Sub UpdateFormats()

' turn off screen updating and automatic repagination to speed things up a bit
Application.ScreenUpdating = False
Options.Pagination = False

Dim strStyle As String
strStyle = "MyStyle"

' reset the styles back to their default settings
Call MyStyleFormat

Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = ""
.ClearFormatting
.Style = strStyle
Do While .Execute
Selection.ParagraphFormat.Reset
Selection.Font.Reset
Loop
End With

' turn everything back on
Application.ScreenUpdating = True
Options.Pagination = True

End Sub
Code:
Sub MyStyleFormat()
'
' MyStyleFormat Macro
' Macro recorded 29/07/2011 by Ben Raynes
'
    With ActiveDocument.Styles("MyStyle").Font
        .Name = "Arial"
        .Size = 10
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineNone
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Scaling = 100
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    With ActiveDocument.Styles("MyStyle").ParagraphFormat
        .LeftIndent = CentimetersToPoints(2.5)
        .RightIndent = CentimetersToPoints(1)
        .SpaceBefore = 6
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceAtLeast
        .LineSpacing = 16
        .Alignment = wdAlignParagraphJustify
        .WidowControl = True
        .KeepWithNext = False
        .KeepTogether = True
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = CentimetersToPoints(-1.5)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
    End With
    ActiveDocument.Styles("MyStyle").NoSpaceBetweenParagraphsOfSameStyle = _
        False
    ActiveDocument.Styles("MyStyle").ParagraphFormat.TabStops.ClearAll
    ActiveDocument.Styles("MyStyle").ParagraphFormat.TabStops.Add Position:= _
        CentimetersToPoints(2.5), Alignment:=wdAlignTabLeft, Leader:= _
        wdTabLeaderSpaces
    With ActiveDocument.Styles("MyStyle")
        .AutomaticallyUpdate = False
        .BaseStyle = ""
        .NextParagraphStyle = "MyStyle"
    End With

End Sub

Obviously, with this method you still have to do a bit of work, as you've got to call UpdateFormats() for each style, but I'm sure I can get it to work by using strStyle as a ByVal. If I get it sorted, and I remember, I'll post the updated method!

Still not sure why just resetting the style information didn't work, but hey! As long as it does what I want it to!
 
Carthesis,

although "doesn't work" is not exactly informative, here's a different approach that should do well. Tried it here and it worked nicely.
Code:
Sub ResetHeadings()

With Selection

    .HomeKey unit:=wdStory
    
    With .Find
        .ClearFormatting
        .Text = ""
        .Format = True
        .Style = ActiveDocument.Styles("Section heading")
        .Execute
    End With
    
    Do While .Find.Found
        .ClearFormatting
        .Style = ActiveDocument.Styles("Section heading")
        .Collapse wdCollapseEnd
        .Find.Execute
    Loop
    
End With

End Sub

Hope this helps,
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo: Yeah, looking back I could have been more specific.

I say "doesn't work", and in this case I mean that the change suggested made no difference to the base functionality of the code I already had, as described in the first post.

The latest suggestion you made bears a striking resemblance to the solution I too found and posted, which I know works because I too have tested it, so thanks! :)
 
LOl! Hadn't seen that!
[tongue]

Glad you got it sorted.
[thumbsup2]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
MakeItSo: As it turns out, your solution is far superior to the one I found, as Selection.ParagraphFormat.Reset and Selection.Font.Reset work on a per character basis within the selection, hence is amazingly slow!

Your last suggestion works much much faster, and therefore is the method I'd recommend to anyone wanting to do something similar. Can be made even faster if you programmatically change out of print view as well, as you can then force automatic repagination to not take place.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top