none
Run-time error '5947': Could not change document template. - Please Help! RRS feed

  • Question

  • Here's my code:

    Sub AttachTemplate()
    Dim appWord As Application, TemplateDir As String, Loc As String
    Set appWord = GetObject(, "Word.Application")
    TemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath) & "\"
    Loc = TemplateDir & "poms2012.dotm"

            With ActiveDocument
            .UpdateStylesOnOpen = True
            .AttachedTemplate = Loc
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With

    End Sub

    I removed extraneous stuff so you could see the logic.  It dies at .AttachedTemplate = Loc.  I have tried removing the folllowing two lines, hardcoding the value of Loc, putting in error checking, nothing works.

    Background: I have several macros kicking off upon document_open.  There is an embedded template in the document that is first placed in the templates folder.  I have a checker to see if the file is open and waits for it to close (this was removed for simplicity).  After reassigning the new template, it fixes the styles with another macro and opens the styles window.  These work fine if I remove the macro that attaches the template. 

    Please help.  I had it working for a day or so a few months ago.  I came in to work the next day and it failed.  I have no idea what happened.  I need it to work in both 2010 and 2007.  Thank you!

    Monday, September 24, 2012 6:01 PM

All replies

  • I have constructed a series of subs and functions (mostly borrowed from the good people on the internet since I'm a newbie at VBA) that extract an embedded macro-enabled template, place it into the user's default templates directory (if it does not already exist), and is supposed to attach the template to the file. I also intend to kick off a macro that will limit the styles (start protection).  This is all being done on a file that was created from a nearly duplicate template (unprotected so it will accept the new template).

    When I open the document, everything works up to the point of attaching the new template.  Then I get the 5947 error. 

    I tried recording a macro to change the template and cut and pasted the code.  It still fails.  However, I did notice that if I run it from the editor it gives me a 5180 and says it can't open the original template and it does change the template.

    Please help!  I don't know what else to try.  Thanks

    Here's some of the code:

    Sub ExtractTemplate()

    Dim Ishape As InlineShape, Loc As String, wDoc As Document, appWord As Application, TemplateDir As String, FileName As String

                Set appWord = GetObject(, "Word.Application")
                TemplateDir = appWord.Options.DefaultFilePath(wdUserTemplatesPath) & "\"
                Loc = TemplateDir & "poms2010.dotm"
    For Each Ishape In ActiveDocument.InlineShapes
      With Ishape
          If .OLEFormat.ClassType = "Word.TemplateMacroEnabled.12" Then
             .OLEFormat.DoVerb VerbIndex:=1
                Set wDoc = ActiveDocument
                If File_Exists(Loc) = False Then
                    wDoc.SaveAs FileName:=Loc
                    wDoc.Close
                Else
                    wDoc.Close
                End If
          End If
        End With
    Next Ishape
    End Sub

    Sub AttachTemplate()
     With ActiveDocument
            .UpdateStylesOnOpen = True
            .AttachedTemplate = _
                "C:\Users\username\AppData\Roaming\Microsoft\Templates\poms2010.dotm"
            .XMLSchemaReferences.AutomaticValidation = True
            .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False
        End With
    End Sub

    Penny



    Friday, May 25, 2012 1:48 PM
  • Hi PenXX

    What's the text with the 5947 error?

    Is the original template to which the file is attached available on the local machine, in a place Word can be expected to find it?

    Which version of Word is involved?


    Cindy Meister, VSTO/Word MVP

    Friday, May 25, 2012 4:02 PM
    Moderator
  • Using Word 2007 and 2010.  I actually embed the template in the document and programmatically place it the user's default Templates folder.  I am able to add it in as and addin, but it refuses to attach it.  the text is:

    Cannot change document template.

    Friday, September 21, 2012 2:58 PM
  • The answer is no, the original template is not available.  Is that the cause of the problem?  How, then, will I ever be able to make this work where someone downloads a file with a restricted set of styles and macros that don't exist on their machine?  I have, as I said, embedded the file that is to serve as the template, but the document needs to be prestyled in the correct styles in order for them to be able to work.
    Friday, September 21, 2012 5:20 PM
  • Hi PenXX

    Set up the document as you do now, but before you put together the "package", make sure that the document's AttachedTemplate is set to NormalTemplate (Normal.dotm). Every installation of Word has that template and can "find" it.

    This won't affect the styles in the document as long as "Update styles from template" is not activated. Once the template is actually available, you can link to it and, if you want that setting, change it.

    Note that a document generally has no link back to a template as far as styles go. The styles in a template are copied to the document when the document is created. Only when "Update styles from template" is activated is there any automatic communication between template and document. This takes place when the document is opened.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, September 22, 2012 6:00 AM
    Moderator
  • Hi Penny

    It would really help things if you could stick to one conversation thread.

    I gave you a suggestion a couple of days ago to which you have not reacted. What was the result of making sure the document is attached to NormalTemplate?

    Are you certain that the template is properly when you extract it? You might test whether the template can be opened in Word with no problems. And whether attaching it works better after it's been opened in Word...


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, September 25, 2012 5:28 AM
    Moderator
  • I gave you a suggestion a couple of days ago to which you have not reacted. What was the result of making sure the document is attached to NormalTemplate?

       -- It still refused to attach the template

    Are you certain that the template is properly when you extract it? You might test whether the template can be opened in Word with no problems. And whether attaching it works better after it's been opened in Word...

      -- I did double check the document and it is perfect.  Tried again to attach it after opening with same result, i.e., it refused to attach.

    The template will not attach even if the normal template is attached or the original template is attached.  I have gotten the following run-time error when running the sub as part of the document_open:  Run-time error '-2147352567 (80020009)': Method 'Run' of object '_Application' failed.   And the 5947 error is "Could not attach document template".  I get the former when I open the document and the latter when I run the sub from the VB editor.

    Is it possible that a patch or group policy has somehow interfered with the ability to attach a template programmatically?

    I really appreciate your help! 

    PennyNS


    Cindy Meister, VSTO/Word MVP, my blog



    Penny S


    • Edited by PennyNS Thursday, September 27, 2012 6:57 PM
    Thursday, September 27, 2012 6:55 PM
  • Hi Penny

    I don't know what's causing the problem, although my guess would be that something is making the document unstable. Whether it's inherently damaged or the problem comes from something to do with the templates or your code I can't judge without actually working on the project, myself.

    However, you might look into a different approach for extracting the embedded object and saving it as a separate file. This came up new the last couple of days:
    http://code.msdn.microsoft.com/CSOfficeDocumentFileExtract-e5afce86

    This should be done on the closed file, so that when it's opened the object is already extracted and available.


    Cindy Meister, VSTO/Word MVP, my blog

    Thursday, October 4, 2012 10:25 AM
    Moderator
  • Hi Cindy,

    I am sure the template (embedded) is not corrupted because when I attach it manually it works fine.  So the method of extracting and saving is working. 

    I have discovered that if I move the AttachTemplate() sub into its own module, I can kick it off manually and it works.  (I have discovered that something, either a patch or policy, strips the macros from the embedded document.  That is okay because I can just load the code onto the document itself.  I actually think this is extremely important since it would be very dangerous to allow Document_open to install a macro-enabled document anywhere it pleased...) 

    All I am really trying to do is reestablish the styles, because they can be corrupted/changed when you edit the document, even though it's a secured document.  And I need to ensure that every person that edits the document has that template in place. I cannot control that if I rely on them to do it manually.

    If only I could rely on the properties of the styles to not change, I wouldn't need to do this at all...


    Penny S

    Friday, October 5, 2012 2:54 PM
  • Hi Penny

    <<I am sure the template (embedded) is not corrupted because when I attach it manually it works fine.>>

    I didn't mean the template, I meant the document in which the template is embedded.

    I get the feeling I'm still missing some key information about how all this has been put together. But as you say this is running in VBA, in Word, on looking at your code again, get rid of this line:
      Set appWord = GetObject(, "Word.Application")

    And remove appWord. everywhere it appears in your code.

    When VBA is running in the application it's controlling "Application" is understood. It's quite possible that getting an outside reference in the mix is part of the problem.

    <<If only I could rely on the properties of the styles to not change, I wouldn't need to do this at all...>>

    You know that, since Word 2003, you can "protect" the document's styles? It's not 100%, but probably as good as updating styles from a template. Go to the Review tab and click "Protect Document". Choose "Restrict Formatting and Editing". You want to first use Step 1, then Step 3.


    Cindy Meister, VSTO/Word MVP, my blog

    Saturday, October 6, 2012 3:42 PM
    Moderator
  • Hi Cindy,

    I was on vacation but I have returned and I thank you for your response.  I removed the appWord from the code and as you suggested, it still works just fine.  However, it still does not attach the template.  If I manually kick off the macro, it DOES attach it, I just can't get it to kick it off programmatically. 

    The document has been protected from the first day.  The problem is that without the template attached the styles are easily corrupted - even when it is locked down.  It adds numbering, changes indents, all sorts of weird stuff.  But it seems to be pretty stable if it has the correct template attached so that is my focus.

    I am about to give up and devise a workaround that checks for the attached template and if it's not the right one, I will make the document read-only.  Then I will create a popup that tells users how to kick off the macro to attach the template to open it for editing.  Any better ideas for dealing with this would be greatly appreciated.

    Penny

    • Marked as answer by PennyNS Monday, October 22, 2012 4:15 PM
    • Unmarked as answer by PennyNS Monday, October 22, 2012 4:16 PM
    Friday, October 19, 2012 1:07 PM