• Code to Import Styles from Global Template (Word VBA)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Code to Import Styles from Global Template (Word VBA)

    Author
    Topic
    #362878

    I finally sat down and wrote the code to import styles from a global template into the current document. It is posted as A Global StyleSheet in Word? (addbalance.com/word/stylesheet.htm for future reference)

    Here’s the code, without all the error handlers and comments:

    Sub StyleCopyMacro()
    ' StyleCopy Macro written by Charles Kyle Kenyon 14 November 2001
    ' Copyright 2001 All rights reserved
        Dim sThisTemplate As String
        Dim sTargetDoc As String
        Dim i As Integer 
        Dim iCount As Integer 
        Dim rResponse As Variant 
        sThisTemplate = ThisDocument.FullName 
        sTargetDoc = ActiveDocument.FullName
        rResponse = MsgBox(Prompt:="This command redefines your Body Text Style and" _
            & vbCrLf & "Heading Styles 1-9. Are you sure you want to do this?" _
            & vbCrLf & vbCrLf & "If you are not sure, answer 'No' and " _ 
            & "make a backup of your document." _
            & vbCrLf & "Then run the command to copy the styles again.", _
            Title:="Are you sure you want to redefine your styles?", _
            Buttons:=vbYesNo + vbExclamation)
        If rResponse = vbNo Then Exit Sub
        For i = 1 To 3 
            With Application
                .OrganizerCopy Source:=sThisTemplate, _
                    Destination:=sTargetDoc, Name:="Body Text", Object:= _
                    wdOrganizerObjectStyles
                For iCount = 1 To 9 
                    .OrganizerCopy Source:=sThisTemplate, _
                        Destination:=sTargetDoc, Name:= _
                        "Heading " & iCount, _
                        Object:=wdOrganizerObjectStyles
                Next iCount
            End With
        Next i
    End Sub
    

    The key for me was the discovery of the ThisDocument object as a way to refer to the global template. That way, you don’t have to know where the user will stick the template or what name it will have, just that the template that contains the macro is the same template that contains the styles.

    Feel free to adopt for your own use, although a reference in the code documentation would be appreciated. Thoughts?

    Viewing 0 reply threads
    Author
    Replies
    • #552236

      Hi Charles,

      I didn’t know about “ThisDocument” – thanks. You can also use “MacroContainer” in that situation.

      Is the “i = 1 To 3” loop the code equivalent of your “import styles three times” dictum?

      Gary

      • #552307

        Yes, the “i=1 To 3” loop copies the styles three times to maintain linkages among styles. Thanks for the tip about “MacroContainer.”

        • #621250

          Quite a bit late to be adding to this thread, I know, but I’m trying to do something similar to this and have an additional question.

          Charles, I have written code essentially similar to yours, except I’m trying to give the user the option to see a full list of all of the styles we’re going to copy. Basically, we have a default normal.dot that we keep on our network (believe me, much as I’d like to not have a modified normal.dot, that isn’t a battle I can fight at the moment). That said, we occasionally add new styles to this network copy of the template, and I need to be able to distribute them to users without over-writing their existing normal.dot.

          I want to do that by providing a macro in a startup template that will (a) show the user the styles available to be updated; ( let the user select the styles she wants to update; and © with a single button, copy the styles (or toolbars, actually) from the network version of the normal.dot to the user’s normal.dot.

          I’m faltering on (a), because I can’t seem to get a list of the styles that exist in the file. I could get a collection of the styles if I opened a document based on the template (for each firmStyle in ActiveDocument.Styles, where the active document is the network normal.dot) but if I Dim myNormal as a Template, I don’t have seem to have a “Styles” collection.

          Is there some way to see the styles attached to a template without actually opening the template? Any thoughts would be appreciated; thanks in advance for your assistance.

          Jen

    Viewing 0 reply threads
    Reply To: Code to Import Styles from Global Template (Word VBA)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: