Solved

Linking styles and numbering using VBA

Posted on 2011-09-22
7
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

697 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