• VBA property to find path of attached template

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » VBA property to find path of attached template

    Author
    Topic
    #485147

    Problem:I have word documents based on templates from 2 different server locations. I have just combined the 2 different locations into one. I need to check all documents and re-write the correct path to the now common workgroup template location.
    What is the correct VBA property to get at the path information? When I use ActiveDocument.BuiltInDocumentProperties(wdTemplateProperty), I see the template name only, not the path.
    When I use ActiveDocument.AttachedTemplate.FullName, I only see the Normal.dot template.
    As an alternative I have thought about scanning the entire document looking for the known incorrect path string but this seems quite inefficient.

    Viewing 3 reply threads
    Author
    Replies
    • #1347646

      Try ActiveDocument.AttachedTemplate.Path. (Path does not display with Intellisense, but if you type the dot and Path, you’ll get what you need.

    • #1347648

      macdonell,

      I’ve successfully switched templates with code like this:

      Code:
      Sub ChangeAttachedTemplate()
      Dim oDoc As Document  ‘make sure you’re getting info on the right document
      Set oDoc = ActiveDocument
      If oDoc.Type = wdTypeTemplate Then Exit Sub
      Dim oTemplate As Template
      Dim strTemplatePath
      Set oTemplate = oDoc.AttachedTemplate
      If InStr(UCase(oTemplate.FullName), “OLDPATH”) > 0 Then
           oDoc.AttachedTemplate = “FULLNEWPATH” & oTemplate.Name
      End If
      End Sub

      The code I used had a few more Ifs, but it works for me.
      Does this help?
      Kim

    • #1348064

      Do your templates rely on some extra pathing? Word should look at it’s default path and go from there. Anything in the “general” directory should not be a problem. If you keep the sub directories the same name when transferring the templates, most should be fine.
      But I do recall some instances where we used a macro to dig out the path to the Workgroup directory, and then appended the name of the subdirectory we needed. Sorry, don’t work there anymore.

      • #1348661

        Thank you for the suggestions. A bit more back ground information about my issue.
        Most of the files in question have been created with Word2000 or 2002.
        At the same time of combining the 2 separate servers (1-win2000 & 1-Win7) into one, I have been upgrading the user PCs with Win7 and Office 2010. This upgrade finished before the all the files were completely moved and combined into one of those existing servers (Win7).
        The users were commenting some Word files took a longtime to open. They were using Word2010 now.

        When I looked, I noticed on the status line, a message indicating the Word was looking for the old server that had been turned off. Eventually a time-out occurred and the file appeared on the users’ screen. I turned the old server back on and the problem went away.
        Starting to dig around, I opened some of the files with Notepad and saw the same path information and template name that was showing on the status line when a file was being opened but the old server was turned off.

        That is when I assumed that if I found the VBA code, I could merely rewrite the WorkGroup Path information of the particular template, that had been used to create the Word document. I thought everything would be fine now.

        I have not been able to find any reference to this item in VBA. There is code to rewrite the Workgroup Path but that resides in the application Word.
        AttachedTemplate.Path or .FullName produced the path and name of the Normal template attached to the file. Finally, I realized that the AttachedTemplate property was not appropriate as the creating template was not attached to the document.
        Out of desparation, using the code idea from Kim, I rewrote the Path for the normal.dotm file that was always shown as the attached template.

        Sub UpdateTemplServPath()
        Dim NTemplate As Template
        If ActiveDocument.Type = wdTypeTemplate Then Exit Sub ‘Do nothing if this is a template. It has to be a document
        Set NTemplate = ActiveDocument.AttachedTemplate ‘Gets name of normal.dotm attached template
        ‘Path is on local PC but is ignored at this point
        ActiveDocument.AttachedTemplate = “\serverDH Templates” & NTemplate
        ‘Path is now rewritten to be new server along with normal.dotm template name
        ActiveDocument.Save ‘document is saved with new path info
        End Sub

        Wonder of wonders, the next time I attempted to open the document, it opened with no delay!
        When I attempted to verify this by using Notepad, I could not find any ASCII readout of the WorkGroup path. I am assuming that this is because the file was rewritten according to the newer Word 2010 format.

        This seems to have worked. I do not understand why. I hope that there are no side effects to this action.
        If someone can shed some light on what is happening inside Word, I would appreciate it.
        Thanks again for simulating my thoughts towards a solution.

        • #1348779

          I have also changed the Path variable and have found that it can be anything I specifiy so long as it is a valid path. When I look at the specified path destination, I never see evidence of the normal.dotm file. I am now even more confused as to what is going on in the background.

          • #1348788

            macdonell,

            A Word document remembers the template from which it was created. When the original template is not available (for example, you move it to your personal laptop), Word temporarily attaches the document to the default Normal template.

            If you go to Developer tab > Templates group > Document Template button, you’ll see at the top of the dialog that it still points to the old location. But if you look at the document in the project pane in the VBE, you’ll see that the document has a (temp) reference to Normal.dotm. When the document returns “home” it will again look for its template. If it doesn’t find it, Word will again attach it to Normal.

            Word works pretty hard to find the template before it attaches Normal. Consider this:

              [*]I created a document based on a template in my workgroup location.
              [*]I closed the document and moved the template to another folder.
              [*]I opened the document and looked in the Document Template dialog and it pointed to the new location.
              [*]I closed the document and moved the template to my desktop.
              [*]The Document Template dialog now showed that BasicDoc was now on my desktop.

            Because each location was on my computer, it all happened pretty quickly. Searching a network could easily account for the time it took your users to open documents. I don’t know the parameters of Word’s search. Mapped drives? When I moved the template off my computer and onto a USB flash drive, Word didn’t find it.

            Ultimately, I believe there should be no side effects to your solution. However, whatever assets were in the original template (styles, autotext/quick parts, macros) will no longer be available to the document. This may not be an issue at your company.

            Was your ultimate goal to attach to the Normal template or to attach to the relocated (original) template? Or just to get the darned things opened?

            I wonder if this might help? I dug up this simple code which lets you return the path and name that appears in the Document Template dialog:

            Code:
            Sub WhichTemplate()
            Dim strTPath As String
            strTPath = Dialogs(wdDialogToolsTemplates).Template
            MsgBox strTPath, vbOKOnly, “Your template path is…”
            End Sub

            …whether the template is still there or not. Whereas ActiveDocument.AttachedTemplate will (as you’ve found) return “Normal.dotm”.

            Best, Kim

    • #1348735

      I’ve run into that before. Word “remembers” the document template that the document or template was based on and searches for it, causing long delays. If you click on the “Developer tab”,and then “Document Template” (doc must be unprotected), the path to the original template will be highlighted. Simply pressing the Delete key will erase it, and saving should cause it to point to your current Normal.dotx and eliminate the problem for that document.
      You’re better at programming than I, so I’ll leave the rest to you.

      Doug Mac

    Viewing 3 reply threads
    Reply To: VBA property to find path of attached template

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

    Your information: