• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

Apply a style from Normal.dot in Word 2003 VBA

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 Normal.dot to make it available to all documents.



Selection.Style = Documents("Normal.dot").Styles("_Body .5""")
  • 2
1 Solution
This is complicated and doesn't make much sense.

In order to apply a style from Normal.dot to a document attached to another template, you would have to open the Normal.dot, 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?
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
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.
jwpinkneyAuthor Commented:
Thanks to all for the comments.  I had a misunderstanding as far as styles existing in the Normal.dot 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 Normal.dot, then used a variation of GrahamSkan's code to copy the style from Normal.dot 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!


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now