?
Solved

Apply a style from Normal.dot in Word 2003 VBA

Posted on 2007-10-08
4
Medium Priority
?
300 Views
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 Normal.dot to make it available to all documents.

Thanks.

joe

Selection.Style = Documents("Normal.dot").Styles("_Body .5""")
0
Comment
Question by:jwpinkney
  • 2
4 Comments
 
LVL 58

Expert Comment

by:harfang
ID: 20038372
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?
(°v°)
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 20039534
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
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 20039554
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.
0
 

Author Comment

by:jwpinkney
ID: 20102032
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!

joe

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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…
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.
Suggested Courses
Course of the Month15 days, 20 hours left to enroll

850 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