Solved

Linking styles and numbering using VBA

Posted on 2011-09-22
7
391 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:teylyn
Comment Utility
Hello,

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

cheers, teylyn
0
 
LVL 2

Author Comment

by:ajakeway
Comment Utility
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:teylyn
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:ajakeway
Comment Utility
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:
teylyn earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now