none
VBA How to close/unload a template in Templates collection RRS feed

  • Question

  • Hi,

    I'm trying to  reference and close programmatically one of the templates in the Templates collection. But I don't kmow how to do it.

    I tryed to get a reference to it but I encounter this strange behavior:

       For Each t In Templates
          Debug.Print t.Name
       Next
       Debug.Print Templates("Normal.dotm").Name

    When the loop is executed everythng is OK, I can see all the templates open in my VBE project manager.

    But when I try to insert the name of every single template I receive the 5941 error (member doesn't exit); only with "Normal.dot" is OK.

    Why? Lauro

    Saturday, October 6, 2018 3:21 PM

All replies

  • OK,

    I partially solved by myself.

    I need to use the full name!

    Lauro

    Saturday, October 6, 2018 3:26 PM
  • The use of

    Documents("MyTemplate.dotx").Close
    with Name or FullName will not work. Error 4160 "File name error"

    Saturday, October 6, 2018 3:41 PM
  • To explain better what I want to achieve.

    With  Templates.LoadBuildingBlocks I load all my templates with the Building Blocks I need, but at a certain point I need to move one template from its location and that is impossible if it's open in Word, so I need first to unload it.

    Thanks, Lauro

    Saturday, October 6, 2018 3:56 PM
  • Not certain this is what you need, but I think you may be better off trying the following:

    Dim a As Word.Addin
    For Each a In Application.Addins
    ' You might be better off checking the full Path & Name, but here we'll just check the Name
      If a.Name = "MyTemplate.dotx" Then
        ' You either need this
        a.Installed = False
        ' Or this
        a.Delete
        Exit For
      End If
    Next
    
    That should also give you a clue as to how to access the relevant Template from within a loop if that is what you need.

    I don't know which of Installed or Delete you really need. However, Delete behaves differently depending on whether the .dotx (or whatever) is in the Startup folder or lsewhere. If it's in the startup folder, it stays in the list of Global templates that Word will load on startup. Otherwise, it is removed.



    Peter Jamieson

    Saturday, October 6, 2018 4:18 PM
  • Thanks Peter, but I made a loop in Apllication.AddIns and nothing will be found.

    I think that the templates avaliable are in Templates not in Apllication.AddIns

    Lauro

    Saturday, October 6, 2018 6:07 PM
  • If it only appears in Templates then I *think* it has to be a template attached to a document. In that case, since there is no UnloadBuildingBlocks method, I think you would have to do something like this:

    Dim d as Word.Document
    Dim t as Word.Template
    For each d in Application.Documents
      Set t = d.AttachedTemplate
      If t.Name = "myTemplate.dotx" then
        ' either...
        d.Close ' and decide what to do about saving
        ' ...or
        d.AttachedTemplate = "some other template" ' put a suitable template name such as normal.dotx or normal.dotm here
      End If
      Set t = Nothing
    Next

    (NB, you do not have to introduce the t Variable but it helps with VBA's Intellisense)


    Peter Jamieson

    Saturday, October 6, 2018 10:03 PM
  • Dear Peter,

    again the loop on Documents shows only one document open and one attached template, I cannot reference in this way all the  templates loaded with Templates.LoadBuildingBlocks

    Ciao Lauro

    Sunday, October 7, 2018 5:02 PM
  • In that case I assume your building blocks are in templates in the same folder as the builtin building blocks template, in which case Word loads them when it starts, but there is apparently no method to unload them. All I can really do is suggest one or two possible workarounds for that. Not good!

    Perhaps there is a way to remove the template while it is still open in Word, but I have not explored that possibility. So I think the best you could do would be to remove all the blocks in the specified template. But that would mean that you

     a. might have to deal with questions about saving the modified template when Word closes, and could potentially lose all the building blocks in the template
     b. might need a way to restore the building blocks without closing word and discarding the changes to the .dotx.

    If you control the building blocks files, then I suppose you could avoid some of the problems of (a) by marking the .dotx as read-only in Windows, but AFAICS restoring entries would probably require you to keep a second copy of the .dotx outside the standard folder.

    So I suspect that if you really need to unload building blocks in the way you are proposing *and* you have control over the locaiton of the templates containing the bloks, the thing to do is avoid putting them in the standard location, and load the relevant templates in your own code.


    Peter Jamieson

    Sunday, October 7, 2018 10:19 PM
  • Yes Peter,

    the templates that are loaded with the Templates.LoadBuildingBlocks are all the folder:

    C:\User\username\AppData\Roaming\Microsoft\Document Building Blocks\

    It seems very strange to me that I have the chance to load the templates with the previous said method but that I cannot unload them.

    As said in a previuos  post I need to remove one template from the folder and put in another one, but the templates could not be moved if are open in Word.

    Lauro

    Monday, October 8, 2018 7:22 AM
  • Hi Lauro2,

    Did you solve the problem? If you solve your problem, could you provide a solution to help more people?

    Best Regards,

    Bruce


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, October 30, 2018 2:17 AM
    Moderator