Apply a style from in Word 2003 VBA

Posted on 2007-10-08
Last Modified: 2012-08-13
I am trying to set the style of the selected paragraph to a particular style.  I could use:

    Selection.Style = ActiveDocument.Styles ...

but that would require me to put the style into every document template where I might ever need it.  I am hoping to build the style into to make it available to all documents.



Selection.Style = Documents("").Styles("_Body .5""")
Question by:jwpinkney
    LVL 58

    Expert Comment

    This is complicated and doesn't make much sense.

    In order to apply a style from to a document attached to another template, you would have to open the, copy the style over to your document and then apply it.

    But it doesn't make sense, because the style your code applies and the definition of that style should really be in the same file, meaning in the same template. If the style "_Body .5" is used in one of your templates, it should not rely on its present in any other template.

    What if you run the same code on another computer, or on the same computer but with a different login? The normal template is basically not a reliable place to store anything at all, save for your very personal toolbars and "quick and dirty" styles.

    If you cannot store the style, you can also recreate it from scratch every time:

        On Error Resume Next
        Selection.Style = "_body .5"
        If Err.Number = 5834 Then
            With ActiveDocument.Styles.Add("_body .5")
                .Font.Size = 18
                With .ParagraphFormat
                    .LeftIndent = CentimetersToPoints(1.5)
                    .Alignment = wdAlignParagraphJustify
                End With
            End With
            Selection.Style = "_body .6"
        End If

    Does that help?
    LVL 76

    Accepted Solution

    You can use OrganizerCopy to pull the style into your document.

    Sub AddAndApplyStyle()
        Dim strStylename
        strStylename = "_Body .5"""
        Application.OrganizerCopy NormalTemplate.FullName, ActiveDocument.FullName, strStylename , wdOrganizerObjectStyles
        Selection.Style = strStylename
    End Sub
    LVL 76

    Expert Comment

    In line with Harfang's caveats, be aware that because many styles are based on other styles in the document, one copied from another document could behave differently in the new document.

    Author Comment

    Thanks to all for the comments.  I had a misunderstanding as far as styles existing in the being available to all other documents--now I understand that they are not available.   Also, that last line after my signature in the original post should have been deleted, as I knew that was not correct code.

    My final solution was to create all of the styles in, then used a variation of GrahamSkan's code to copy the style from to the active document.  The code is initiated from a menu, so my user's can apply the custom styles with two mouse clicks.

    Thanks again!



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Shortcuts in Word Just the other day I had a training for Microsoft and they wanted me to show how well the new Windows and Office behaved on a touch device, which by the way is great, but it was only then that I realized that using keyboard shortc…
    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…
    This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
    Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

    794 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