Solved

Linking styles and numbering using VBA

Posted on 2011-09-22
7
394 Views
Last Modified: 2012-06-22
I have about 30 templates where I want to, using VBA, ensure that they have a consistent user-defined style and numbering format.  I managed to get the font and sizing correct for the style, but the numbering seems to be a separate beast.  Can anyone supply VBA code that says, "when I apply this style, use this numbering".  Basically, like how it works when you use the built-in "Header1" or "Header2" style format.  But for me in a custom style.

Thanks!
0
Comment
Question by:ajakeway
  • 4
  • 3
7 Comments
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36579991
Hello,

why don't you include the numbering in the style definition?

cheers, teylyn
0
 
LVL 2

Author Comment

by:ajakeway
ID: 36580014
Teylyn,

I would love to.  I have spent two days searching online for how to do so.  Can you provide any code or website that might guide me on this?  Unfortunately the macro recorder hasn't helped me with this either.

Alex
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 36580094
Modify the style and select a numbering format.

Or are you defining the styles from scratch in VBA? If so, why? You might consider creating templates with the styles already defined and create documents based on these templates. Then the styles and their numbering will be in place without any VBA involvement.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 2

Author Comment

by:ajakeway
ID: 36580115
Because I am modifying templates, I thought it best to define the styles from scratch in VBA.  That way there would be consistency between the templates, and I could use VBA to open each template, apply the required style (and numbering scheme) and then save the template.  I could do so manually, but I foresee us taking this approach for other documents (and templates) as well, so if possible, doing it via VBA would be nicer.
0
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst earned 500 total points
ID: 36580140
In Word 2010, the macro recorder creates this when modifying the Heading1 style and assigning the numbering format

A.
B.
C.

Sub Macro1()
'
' Macro1 Macro
'
'
    With ActiveDocument.Styles("Heading 1").ParagraphFormat
        .LeftIndent = CentimetersToPoints(1.27)
        .RightIndent = CentimetersToPoints(0)
        .SpaceBefore = 24
        .SpaceBeforeAuto = False
        .SpaceAfter = 0
        .SpaceAfterAuto = False
        .LineSpacingRule = wdLineSpaceMultiple
        .LineSpacing = LinesToPoints(1.15)
        .Alignment = wdAlignParagraphLeft
        .WidowControl = True
        .KeepWithNext = True
        .KeepTogether = True
        .PageBreakBefore = False
        .NoLineNumber = False
        .Hyphenation = True
        .FirstLineIndent = CentimetersToPoints(-0.63)
        .OutlineLevel = wdOutlineLevel1
        .CharacterUnitLeftIndent = 0
        .CharacterUnitRightIndent = 0
        .CharacterUnitFirstLineIndent = 0
        .LineUnitBefore = 0
        .LineUnitAfter = 0
        .MirrorIndents = False
        .TextboxTightWrap = wdTightNone
    End With
    ActiveDocument.Styles("Heading 1").NoSpaceBetweenParagraphsOfSameStyle = _
        False
    With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingTab
        .NumberStyle = wdListNumberStyleUppercaseLetter
        .NumberPosition = CentimetersToPoints(0.63)
        .Alignment = wdListLevelAlignLeft
        .TextPosition = CentimetersToPoints(1.27)
        .TabPosition = wdUndefined
        .ResetOnHigher = 0
        .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 1"
    End With
    ActiveDocument.Styles("Heading 1").LinkToListTemplate ListTemplate:= _
        ListGalleries(wdNumberGallery).ListTemplates(1), ListLevelNumber:=1
    With ActiveDocument.Styles("Heading 1")
        .AutomaticallyUpdate = False
        .BaseStyle = "Normal"
        .NextParagraphStyle = "Normal"
    End With
End Sub

Open in new window


I guess the crucial commands are

   With ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
        .NumberFormat = "%1."
        .TrailingCharacter = wdTrailingTab
       .NumberStyle = wdListNumberStyleUppercaseLetter
0
 
LVL 2

Author Comment

by:ajakeway
ID: 36601450
my apologies, I got tied up with kids duty and then other work became a priority.

As for this... I tried the suggested VBA and it would work in one template but not another.  I believe this is because of differences between the numbering formats in the various templates.  With the reply above and the various other online sources detailing how numbering lists work and the rather, uh, ingenious ways that WORD handles them and styles, I have decided to try approaching this from another way.  If I find a solution, I will post it.  For now, the question is deemed closed.
0
 
LVL 2

Author Closing Comment

by:ajakeway
ID: 36601459
Considering the complexity of styles and numbering lists in Word, the solution provided appropriate guidance.  Ultimately I will need to take a different approach.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Outlook Free & Paid Tools
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question