Results 1 to 4 of 4
  1. #1
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Code to Import Styles from Global Template (Word VBA)

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

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

    <pre>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
    </pre>




    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?
    Charles Kyle Kenyon
    Madison, Wisconsin

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Code to Import Styles from Global Template (Word VBA)

    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

  3. #3
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: Code to Import Styles from Global Template (Word VBA)

    Yes, the "i=1 To 3" loop copies the styles three times to maintain linkages among styles. Thanks for the tip about "MacroContainer."
    Charles Kyle Kenyon
    Madison, Wisconsin

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Chicago, Illinois, USA
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code to Import Styles from Global Template (Word VBA)

    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; ([img]/forums/images/smilies/cool.gif[/img] 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •