Results 1 to 15 of 15
  1. #1
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I’m struggling with a macro (based on Charles Kenyon’s example on his website) to copy styles from my global template to documents created by the user. The macro will be started by an event handler in the global template.

    To speed things up I want to copy only the custom styles I have made; I believe I can locate them by parsing the style name to identify a prefix I use in custome style names. I attempted to step through all the styles with “for each <style> in <global template.styles>” so I could examine each one for the prefix, and then copy it.

    The problem is that the compiler won’t accept “for each <style> in <global template.styles>” presumably because the global template is not active.
    I’d like to avoid a hard-coded number in “for <count> 1 = <total number of custom styles> so that the macro doesn’t need updating if new styles are added.

    I’d be grateful for any suggestions! Or, being told that this is one more Word impossibility...
    Stylus

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    The Styles collection applies to a document only, not to a template, so you should either create a new document based on the global template or open the global template as a document. You can then loop through its styles:
    Code:
    Dim tpl As Template
    Dim doc As Document
    Dim stl As Style
    For Each tpl In Templates
      If tpl.Name = "MyGlobalTemplate.dot" Then
    	Exit For
      End If
    Next tpl
    Set doc = tpl.OpenAsDocument
    For Each stl In doc.Styles
      Debug.Print stl.NameLocal
    Next stl
    doc.Close SaveChanges:=False
    (If you know the exact path of the global template, you can open it directly. The method above has the advantage that you don't have to specify the path)

  3. #3
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    This could be a lot simpler and faster if you did it another way.
    1. Store the name of the attached template in a variable
    2. Attach the global template
    3. Refresh the styles using -- ActiveDocument.UpdateStyles -- which imports ALL styles from the attached template without looping
    4. Reattach the original template

    Aircode for that task
    Code:
    Dim sTempPath as string, sGlobalPath
      sGlobalPath = "c:\folder\globaltemplate.dot"
      With ActiveDocument
    	sTempPath = .AttachedTemplate
    	.UpdateStylesOnOpen = False
    	.AttachedTemplate = sGlobalPath
    	.UpdateStyles
    	.AttachedTemplate = sTempPath
      End With
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  4. #4
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you both!

    I opted for Andrew's solution because it happens out of harm's way. And speedy is right - there can't be more than a second of delay in a template opening.

    I had to make one slight change to the code to make it work:

    With ActiveDocument
    sTempPath = Application.Dialogs(87).Template
    .UpdateStylesOnOpen = False
    .AttachedTemplate = sGlobalPath
    .UpdateStyles
    .AttachedTemplate = sTempPath
    End With

    This way I get the whole file path, which is needed for workgroup templates out on the server.

    Thanks again.
    Stylus

  5. #5
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Another thought:

    How can I return the whole filepath for the global template?

    I version-number the global template, so if I could avoid hard-coding the global filepath, then the macro would be maintenance-free.

    I can return the filepath to the startup folder with "Word.Options.DefaultFilePath(wdStartupPath)".

    I can return the name of the global template with "Application.AddIns.Item(1)" - but there's the rub: it's indexed. What happens if in some unforseen manner there is more than one AddIn?

    I sense that while Word knows which document template it's using (because there can only be one), Word doesn't know specifically about the global template, but instead knows generally about AddIns, because there may be more than one AddIn for a given ActiveDocument.

    Again, I'd be happy for any ideas!
    Stylus

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    A user could have more than one add-in (=global template), so you can't rely on the one you need being the first one. Is the first part or the last part of the filename fixed?

  7. #7
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Hans:

    The first two letters of the filename are standardized, so I suppose I could work with that...

    Loop through Application.AddIns.Item(index) and check for those leading letters.

    I take it that there is no better way then?
    Stylus

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    [quote name='Stylus' post='768423' date='31-Mar-2009 13:10']Loop through Application.AddIns.Item(index) and check for those leading letters.[/quote]
    That's what I would do...

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    What else are you doing with the Global template?

    If you are ONLY using it for the styles it contains then there is no point in having it as a global template anyway. It would be easier to just put it in the user templates directory and then it is easier to find. If you are using it for other purposes then you could make two templates and put the styles into one template in the user templates path and the other template in the startup directory to serve the macro that imports the styles.

    I don't really see the point of problem you are trying to solve - global templates are not generally intended to carry styles for reuse - that's what we use attached templates for. If you want to change the styles of a document to reflect the styles in a template then why would you not put those styles in the attached template?

    Following the global template location problem, isn't your implementation controlled in some way so that you KNOW where the global template is located or are your users manually installing it anywhere they want and then adding it to the addins manually.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    My global template has a custom ribbon with menus that apply the styles. I have cobbled this together with the (great) help of Greg Maxey’s site http://gregmaxey.mvps.org/Ribbon_Ima...bels_Part1.htm Look around in his site, there are many pages about customizing the ribbon it's a whole tutorial.

    I’m relatively new in my current job, and find myself in a corporation, large by local standards, that appears to be virgin territory. I see no sign that there have ever been any templates or styles in use. Each document has been “hand made”, and turning on formatting marks reveals what struggles people have had.

    In my previous job I created a global template with an 03 toolbar and macros. (Boundless thanks to all posters here, for all their unwitting help as I lurked around…) In the end I had some three dozen workgroup templates with styles. Keeping track of it all was a pain. In my current job there could eventually be a much greater number.

    What I am aiming for is to have one single source for all styles, which will limit the process of adding or changing styles. If I put the styles in the global template, then, as a side benefit, I can restrict access to the startup location, yet allow write access to the workgroup template folder. This will allow designated people in each department to create their own workgroup templates (with layout, etc) in their own subfolders. It will work if the templates themselves are read-only.

    Both the global and workgroup templates are located in a specific area that I got set up on a server. However, in my last job the entire drive that contained global and workgroup templates was shut down, following orders from upon high. I then got the network guys to create a virtual drive exclusively for these templates, and had quite a time dealing with the changed pathways of past and present templates.

    Since then I like to code with as great a degree of paranoia as I can manage. I do in fact foresee a possible change to the current location. I do not foresee more than a single item in the startup folder, but who knows. I’d rather build the macro so I don’t have to rewrite it in the future!

    As for different appearances in different documents, my current thinking on that will be the topic of a separate thread.

    I’m open to being clobbered: if my current plan is just plain stupid, please tell me. As it is, I’ve got the macro working; it loads the styles into any document created with a template having the company’s prefix.
    Stylus

  11. #11
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    Thanks for your well thought out description of how you have set this up. Whilst it is not exactly how I would do it, I can see that your approach makes sense and could be made to work. You can get the full path to any addin by using something like this
    Code:
      Dim tpl As AddIn
      For Each tpl In AddIns
    	MsgBox tpl.path & "\" & tpl.Name
      Next tpl
    My own approach is to always store the styles inside the attached template and only rely on addins for macros and toolbars. Rather than have a whole series of templates each using the same styles, I combine these all into a single template (as an attached template) and provide a method to choose from a list of autotext entries (also contained in the template) to bring in the specific content required. This approach allows a single file for maintenance of all styles and content and simplifies the users choice - they just have one template to chose from. I put all the toolbars and macros for users in that same template and don't bother with add-ins for document formatting tasks.
    The only thing that remains is allowing workgroups to maintain their own folder of templates. I do this by just adding a shortcut to that folder in either their User Templates or their Workgroup Templates location. This will group those templates together and show a hierarchy of templates to the users.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  12. #12
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thank you for your thoughts and the code snippet! I will see if I can get it to work, though I wonder if that instance of "for each" will work in a global...

    [quote name='Andrew Lockton' post='768600' date='01-Apr-2009 12:27']I put all the toolbars and macros for users in that same template and don't bother with add-ins for document formatting tasks.[/quote]
    Doesn't that put the toolbar & macro in each document that is created?

    As for having one single template - how do you handle documents that have landscape orientation, or which use paper sizes (significantly) different from Letter or A4, for example Ledger or A3?

    Does your shortcut method give them access to write-protected folders, or is the ultimate location write-enabled?
    Stylus

  13. #13
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    [quote name='Stylus' post='768614' date='01-Apr-2009 22:03']Doesn't that put the toolbar & macro in each document that is created?
    As for having one single template - how do you handle documents that have landscape orientation, or which use paper sizes (significantly) different from Letter or A4, for example Ledger or A3?
    Does your shortcut method give them access to write-protected folders, or is the ultimate location write-enabled?[/quote]The toolbar and macros are contained in the template, not the document that is created from it. Any time a user detaches the template or doesn't have the template, the toolbar and macros disappear - but the content and formatting isn't affected.

    Different paper sizes are done either by code associated with the autotext entries or more often by including section breaks in the autotext entries - since a section break is where the page size, orientation and header/footer data resides.

    The shortcuts don't override the windows folder permissions - people can have read access, write access or no access as your administrators decide. Just because someone can see the shortcut in a network folder doesn't mean they have access to the files contained in the target.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  14. #14
    2 Star Lounger
    Join Date
    May 2006
    Location
    Currently in Europe
    Posts
    103
    Thanks
    7
    Thanked 0 Times in 0 Posts
    I just made an intersting dicovery about the Lockton Method of copying styles from the global template to a new document.

    Briefly attaching the global will do the following:

    1) Overwrite any "normal" styles that have been changed.

    2) Add any custom styles that exist in the global but do not exist in the new document.

    3) Overwrite any custom styles of the same name that exist in both global and document.

    But -

    4) Leave in place any custom style in the new document if that style does not exist in the global.

    In other words, what is happening is not a total over-write; it's more like a synchronization.

    This opens up for interesting possibilities...

    PS Your code snippet does the job. Thank you again.
    Stylus

  15. #15
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts
    [quote name='Stylus' post='768645' date='02-Apr-2009 00:58']Briefly attaching the global will do the following[/quote]It's good to see you are paying attention

    Just to be very clear, simply attaching the global template itself is not responsible for those actions, it is the UpdateStyles that does all that for you. The code I posted actually makes sure that styles don't refresh as you open a document but having that option ticked will carry out all those actions (with the attached template) every time you open the document. I dislike this automation so I turn it off and only refresh the styles when I choose to.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

Posting Permissions

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