Consulting

Results 1 to 5 of 5

Thread: Attach template refused with error 5947 Could not change document template

  1. #1

    Attach template refused with error 5947 Could not change document template

    If I try to attach an already attached template to a document, this is granted.
    If I try to attach another template to a document is ends in error 5947 and no change is made.

    Why would I try to sweep a carpet under a document?
    Originally the document is made based on a template X.dotm. So this X gets known as being attached (ActiveDocument.AttachedTemplate).
    If this document is handed to someone on a different pc who also has those templates but likely in a different (trusted) location (say, D:\templates\X.dotm), there is a need to reattach to that template so all functionality and QAT buttons remain valid.
    This can be done manually, but I prefer to do it from an AutoOpen routine that checks if the current attached template exists and if not, reconnects to the proper one. (This introduced an additional task to remove the non existent template from the Tools>References list as MISSING which caused the VB compiler to suddenly bark at innocent statements).

    I don't seem to be able to use Activedocument.AttachedTemplate = newtemplate without running into this 5947 problem.
    Which is odd, because an initial effort as "proof of concept" with an almost empty file based on one of two almost identical almost empty templates X1 and X2 did allow me to swap between the two. Which made me think that swapping would be a no-brainer.
    But it now is not. Why?
    Last edited by deklerkt; 05-03-2018 at 03:58 AM. Reason: additional clarification

  2. #2
    The logical explanation is that you are running code from a template which you are then removing from the document to attach another template. The code will undoubtedly stop at that point.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    That is what I tried preventing from happening: when a new Document1 is created from the template (double click) the template module containing AutoOpen is copied into the Document1 project and saved as local part of that file when saved as, say, File1.docm. Then, removing the template file and opening File1.docm it obviously misses its template (Normal is usually substituted). But upon opening its own AutoOpen routine is run and that should find the template again (since it is always supposed to be in Environ("templatelocation") ). It finds it, tries to do a .AttachTemplate and that fails with the given error code. The template is in a "trusted location".

    But some further investigation...starting to wonder after your comments...
    It turns out that the AutoOpen is always done on the template routine. And if the template is gone, no AutoOpen is performed (not the File1.docm own AutoOpen) since there is a MISSING template.dotm file in the Tools>Reference. Only after unchecking this, closing the file and reopening it, does it run its local AutoOpen.
    So I face a problem that the MISSING reference must be removed before the File1.docm opens and can run its own AutoOpen.
    I need to do some more tinkering to see if I can somehow circumvent getting a MISSING reference. Perhaps removing the attached template just before closing in AutoClose... back to th drawing board!
    Last edited by deklerkt; 05-03-2018 at 03:21 PM. Reason: additional findings

  4. #4

    I think I got it

    I think I nailed it:
    1) Create a dotm template
    2) Add an "openclose" module containing AutoOpen and AutoClose. Whatever goes in there will be executed by a document based on the template
    2) Add a "TemplateSpecific" module containing an AutoNew module. This is executed when a new document is created based on template. All it does is copying the "openclose" module to the newly created document project
    3) Because the AutoOpen and AutoClose routines are initially only in the template project, they will get executed although most of the code is meant for the document and not the template. However:
    a) AutoClose: if not the .dotm template: remove the template attachment (this avoids MISSING references upon opening, but also lacks a few libraries that must be added programmatically upon AutoOpen)
    b) AutoOpen: if not the .dotm template: attach to template known to exist in a predetermined location (different on any pc, but pointed to through an environment variable that can be set differently for each pc). This requires adding the Scripting library programmatically and using the FileSystemObject through late binding

    I enclose the basics needed in the attached .dotm (that I had to rename .docm to be accepted for upload). A lot of "Debug.Print" statements have been left/inserted to show the steps taken.
    Attached Files Attached Files

  5. #5
    It works fine, but two new "problems" arise to which I have no answer:
    1) if you intend to close the file (using File>Save As of the "X" icon at the top) and then decide to not close (cancel) the AutoClose routine has already worked and set the template to nothing (""). This should not happen, but I see no way to trap the cancel choice and act by either not removing the template attachment or by re-instating it
    2) If the template has QAT buttons, once a document based on it is re-opened all QAT buttons appear twice (I guess this applies to template made ribbons too). That also should not happen but no idea how to prevent it.

    If someone has a suggestion, I'm all ears!

Tags for this Thread

Posting Permissions

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