Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    May 2002
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Attached Template Paths - Global Change (EN / MS Word 97)

    We've recently changed our main server name and virtually all our Word documents have an "attached template" path referencing the old server (templates are stored on the server, not locally). We've setup an alias server-side so Word doesn't hang every time one tries to open an old document - however, I would prefer if there was a 'reasonable' solution to changing the attached template path to the correct path (as this alias is likely to be forgotten everytime the server is rebuilt). Any suggestions on how this could be accomplished using a global macro that would run whenever a user opened a document? There are hundreds of documents affected by this -- everything that was created using our corporate templates prior to the server change. The Workgroup Templates path under TOOLS - OPTIONS has been changed to reflect the new server, but it appears that the "attached template" path functions independant of this setting and is document specific.
    Any suggestions would be appreciated.

    Thx.

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Template Paths - Global Change (EN / MS Word 97)

    Use an AutoOpen macro to reset the attached template.

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

    Re: Attached Template Paths - Global Change (EN / MS Word 97)

    You're correct that the path to the attached template is hard-wired into the document and therefore must be changed in each document.

    There are a couple of different ways to set up code to update the template paths; neither of them are simple:

    - create a macro that will loop through all the files in a directory, opening each one in turn, checking the attached template path and if necessary, updating it, saving and closing the document - before moving on to the next.

    - create a procedure that will run every time a document is opened, which will then similarly check the attached template path and update if necessary.

    If you do the latter, the AutoOpen macro approach suggested by Howard probably isn't appropriate here, because you would need to put a different AutoOpen into each individual template. Also, if the documents at some point can no longer 'see' their attached template on the server, then of course that AutoOpen code isn't going to run anyway.

    The better way to do this is to have code that runs every time a document is opened, in a global template. In order to do this, you can't use AutoOpen but instead need to create a class that can respond to an application-level Document Open event.
    The code in the application-level DocumentOpen event procedure can call a procedure in a standard module which does the actual checking and updating of template paths.

    The following code will return a string containing the template path as it's listed in the Templates & Add-Ins dialog:

    <pre> Set diaTemplates = Dialogs(wdDialogToolsTemplates)
    sOldTemplatePath = diaTemplates.Template
    </pre>

    You can then use the value of the string to compare against your old and new template paths to determine whether updating is necessary. You can use code like this to update the attached template:

    <pre>ActiveDocument.AttachedTemplate = sNewTemplatePath</pre>

    Hope this at least provides a start.

    Gary

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    New York
    Posts
    214
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Template Paths - Global Change (EN / MS Word 97)

    (apologies for the long listing, but this is an important question and a relevant fix.)

    Create a new Template, named, say FixTemplatePath.dot

    <pre>'
    '1.Create a module named library.
    'Paste in:
    '
    Dim x As New EventClass
    Sub AutoExec()
    Call Register_Event_Handler
    End Sub
    Sub Register_Event_Handler()
    Set x.App = Word.Application
    End Sub
    Sub AutoClose()
    Set x = Nothing
    End Sub

    '
    '2.create class module named EventClass
    '
    'Paste in:
    Option Explicit
    Public WithEvents App As Word.Application

    Private Sub App_DocumentChange()
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Dim AttachedTemplate As String
    Dim TemplateDir As String
    Dim TemplateName As String
    Dim Base As String
    Dim BestGuess As String
    Dim SubDir As String
    Dim MyDlg As Dialog

    On Error GoTo oops
    Set MyDlg = Dialogs(wdDialogFileSummaryInfo)

    AttachedTemplate = MyDlg.Template
    'A debug string for testing... Uncomment to step through and see what happens
    'AttachedTemplate = "D:Program FilesMicrosoft OfficeTemplatesbogusthis.dot"
    'name of attached template
    TemplateName = GetFileNameOnly(AttachedTemplate)
    'Path of attached template
    Base = GetPathOnly(AttachedTemplate)
    If Dir(AttachedTemplate) = "" Then 'Current saved template not found
    'Current Word pointer to Template Directory
    'Or you could hard code this
    'or this could be the workgroup template path
    TemplateDir = Options.DefaultFilePath(wdUserTemplatesPath)
    'If the saved template is below the base template folder
    SubDir = Mid(Base, InStr(UCase(Base), "TEMPLATES") + 9)
    'Best guess for the new, correct template path
    'based on wdUserTemplatesPath
    BestGuess = BuildPath(TemplateDir & SubDir, TemplateName)
    'build best guess
    'Stop '- good place to stop if debugging string stuff.
    If Dir(BestGuess) <> "" Then ' got it so attach it
    'needs to be attached twice. Don't ask me why.
    ActiveDocument.AttachedTemplate = BestGuess
    ActiveDocument.AttachedTemplate = BestGuess
    'update styles if LinkStyles is true in Templates and Add-ins
    If MyDlg.LinkStyles = 1 Then ActiveDocument.UpdateStyles
    Else 'failed to do it automatically, so show message box
    Msg = "The document you just opened is supposed to be attached to the template [" & _
    MyDlg.Template & "] It is has been moved or deleted." & vbCrLf & vbCrLf & _
    "Do you want to display the Templates and Add-ins" & _
    " dialog in order to manually locate the proper template"
    Style = vbYesNo + vbCritical + vbDefaultButton1 ' Define buttons.
    Title = "TEMPLATE NOT FOUND" ' Define title.
    Response = MsgBox(Msg, Style, Title)
    If Response = vbYes Then ' User chose Yes. Show templates and add-ins
    Set MyDlg = Dialogs(wdDialogToolsTemplates)
    MyDlg.Display
    ActiveDocument.AttachedTemplate = MyDlg.Template
    ActiveDocument.AttachedTemplate = MyDlg.Template
    If MyDlg.LinkStyles = 1 Then ActiveDocument.UpdateStyles
    End If
    End If
    End If
    oops:
    Exit Sub
    End Sub

    Public Function GetFileNameOnly(sPath) As String
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    GetFileNameOnly = fs.GetFileName(sPath)
    End Function

    Public Function GetPathOnly(PathAndFile) As String
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    GetPathOnly = fs.GetParentFolderName(PathAndFile)
    End Function

    Public Function BuildPath(sPath, sFile) As String
    Dim fs As Object
    Set fs = CreateObject("Scripting.FileSystemObject")
    BuildPath = fs.BuildPath(sPath, sFile)
    End Function
    </pre>


    Save the FixTemplatePath.dot and copy it to each of your users Startup Path.

    Should make a good guess at the right path, and prompt to attach if it's wrong.

    (Note: debugging/testing this is a little tricky, since the main module is attached to App_DocumentChange. what I do is take that entire module (and the support functions below) and copy into another module or template to step through)

  5. #5
    New Lounger
    Join Date
    May 2002
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Template Paths - Global Change (EN / MS Word 97)

    Guy - THANK YOU SO MUCH!

    I haven't had a chance yet to try this out - but hopefully in the next week will have an opportunity to give it a go.

    Regards,
    Andrea

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Attached Template Paths - Global Change (EN /

    I thank Guy too. I hope to experiment this weekend.

    I read your original post with interest. My situation is similar to yours:

    I have a slew of templates which each reference a utility template (Utils.dot). On my development machine, Utils lives D:HERE and so all references from the application templates are to D:HEREUTILS.DOT.

    When I export the application and Utils template to my other machine, or to a colleagutes machine, D:HERE does not exist.

    I am compounded by wanting to distribute these templates locked, with pasword protection, so the locked MyAPP.DOT cannot be opened for change. The invalid reference to D:HEREUTILS.DOT within mYApp.DOT is a compilation error, and so MyApp.dot cannot execute to heal itself.



    I am experimenting this morning with a production process that first installs the templates in C:TEMP, then locks them, sends them to another machine's C:TEMP (runs them to test that they work) and then drags them to an ultimate destination fodler. I have noticed that Windows/Word somehow magically seems to know that templates have been dragged, and updates the registry? The do-si-do is that Utils.dot or any other template can always be registered (manually or automatically) by navigating through Normal.dot the Tools references command in VBE.

Posting Permissions

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