none
File not Found error when opening Word Macro Template RRS feed

  • Question

  • I occassionally receive an error after working in a Word 2007 macro template.  The error is simply "File not Found" but the problem is that the entire code module has been removed from the template and i do not know how to get it back. 

    I have have a macro template that i have several code modules and forms in "WordTools.dotm".  it has no visual design to it as it is used as a global tools template (a shortcut to this template is placed in the microsoft office\startup folder).  i am also compiling and signing the code with a digital signature before saving.

    99 times out of 100 this template will open and save fine, but occasionally this error occurs and i lose all code (thankfully we use Subversion).  after the error dialog is closed the template appears to open ok, but is is in fact missing the entire code project.  in the VBA IDE there is nothing, not even a project for this template.  the size of the template in expolrer is also greatly reduced (from +500k to 60k)

    3 questions...

    why is this happening?

    how can i prevent it from happening?

    how can i get my code back when it happens?

     

    Windows XP SP3

    Word 2007 (12.0.4518.1014)

    Friday, April 1, 2011 3:29 PM

Answers

  • Strangely, it was only yesterday that I came across a situation where all of the code appeared to have disappeared from a template (I say appeared because it was not really the case).

    The situation was that I had a fifty off page template that when opened was to display a userform that was populated with some default values that were stored as document variables in the template and the population was done by the initialize routine in the userform.

    When I tried to create a new document from the template, the Userform did not appear and also, there appeared to  be no macros in the template (there were in fact 3 of them, the autonew and a couple of others that were not really necessary for the operation of the template, but were used during its creation, one of them being to set the initial values of the document variables)   What turned out to be causing my problem was a difference in spelling of a variable name between the code that was setting the intial values and the code in the userform initialize statement that was retrieving those values, which was "erroring out" because it was trying to set the text property of a control on the userform to the value of a variable that did not exist.  While I was able to produce an error message and locate the error by stepping through the code, when the autonew macro was run when using File>New to create a document from the template, the new document was created, but the userform was not shown and there appeared to be no macros in the template nor in the VBE when accessing the template to which the reference was created.

    Using File>Open however to open the template, all of the code was there so I was able to step through it and fix it.

    That however is the first time since the advent of UserForms in Office 97 (so about 12 or so years now) that I have ever come across that happening and I have never heard of anyone loosing code as you have reported.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Matt Cope" wrote in message news:859b0c1d-e3a1-4655-838a-3075e72461c8@communitybridge.codeplex.com...

    thanks doug

    so if there was a possibillty that it is secuirty settings and an untrusted location issue, how should i go about addressing the problem? im not sure what locations i can set as trusted.  also, i have signed the code with a valid digital signature.

    i have tools to export all of my code modules and i do this regularly, along with keeping the templates in source control, and this certainly helps.  but this problem happens so randomly that i inevitably can lose several hours of work as there is no indication of this problem about to happen until i have closed the template and then reopen it

    possible corruption, yes i think this could be the case but as i mentioned before the templates only contain code and dont have any design elements in them (ie no content and no changes to the styles etc).

    something else that i have noticed about when this happens but i am neither 100% certain this is the case or if it is purely coincidence and would just confuse the issue.....   the problem possibly only happens if i have not ensured that the code project compiles fully before i save the template and i save it from within word and not within the IDE.

    thanks for your time and sugestions doug, but i can see by the lack of other response to my problem that im probably experiencing something that nobody else has and maybe i need a complete machine rebuild and office install and see if it happens after that?

    matt


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 10:07 AM
    Tuesday, April 5, 2011 8:14 PM

All replies

  • Place the template itself in the Word startup folder.  It will then be treated as a global add-in when Word is started and the macros in it will be available for use with all documents.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Matt Cope" wrote in message news:959d9b0d-37bc-4be4-84f0-ee9b49081386@communitybridge.codeplex.com...

    I occassionally receive an error after working in a Word 2007 macro template.  The error is simply "File not Found" but the problem is that the entire code module has been removed from the template and i do not know how to get it back.

    I have have a macro template that i have several code modules and forms in "WordTools.dotm".  it has no visual design to it as it is used as a global tools template (a shortcut to this template is placed in the microsoft office\startup folder).  i am also compiling and signing the code with a digital signature before saving.

    99 times out of 100 this template will open and save fine, but occasionally this error occurs and i lose all code (thankfully we use Subversion).  after the error dialog is closed the template appears to open ok, but is is in fact missing the entire code project.  in the VBA IDE there is nothing, not even a project for this template.  the size of the template in expolrer is also greatly reduced (from +500k to 60k)

    3 questions...

    why is this happening?

    how can i prevent it from happening?

    how can i get my code back when it happens?



    Windows XP SP3

    Word 2007 (12.0.4518.1014)


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Friday, April 1, 2011 9:48 PM
  • thanks doug, but i think you're misunderstanding the problem here.  my template loses all of the code in it!  there is no VB project when i open the template and open the VBA IDE.....  there are no code modules, forms or classes.  nothing.   the template size shrinks to 38k, which also confirms that the code has been removed.

    whether or not the actual file is kept in the startup folder or the shortcut is irrelevant as i am opening the template itself from another folder in my c drive.

    i need to know why the entire code project gets removed, how to prevent this (if possible) and how can i get it back?

    thanks

    matt

    Monday, April 4, 2011 9:03 AM
  • Maybe it has something to do with the Security Settings and the template being in an untrusted location.

    Maybe it is corrupt in some way.  If you can on occasion open it when it is intact, try exporting the Visual Basic components and then create a new template and import them into that.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Matt Cope" wrote in message news:0c989f8e-f55a-4d39-a94c-43b2da245b3c@communitybridge.codeplex.com...

    thanks doug, but i think you're misunderstanding the problem here.  my template loses all of the code in it!  there is no VB project when i open the template and open the VBA IDE.....  there are no code modules, forms or classes.  nothing.   the template size shrinks to 38k, which also confirms that the code has been removed.

    whether or not the actual file is kept in the startup folder or the shortcut is irrelevant as i am opening the template itself from another folder in my c drive.

    i need to know why the entire code project gets removed, how to prevent this (if possible) and how can i get it back?

    thanks

    matt


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Monday, April 4, 2011 7:43 PM
  • thanks doug

    so if there was a possibillty that it is secuirty settings and an untrusted location issue, how should i go about addressing the problem?  im not sure what locations i can set as trusted.  also, i have signed the code with a valid digital signature.

    i have tools to export all of my code modules and i do this regularly, along with keeping the templates in source control, and this certainly helps.  but this problem happens so randomly that i inevitably can lose several hours of work as there is no indication of this problem about to happen until i have closed the template and then reopen it

    possible corruption, yes i think this could be the case but as i mentioned before the templates only contain code and dont have any design elements in them (ie no content and no changes to the styles etc).

    something else that i have noticed about when this happens but i am neither 100% certain this is the case or if it is purely coincidence and would just confuse the issue.....   the problem possibly only happens if i have not ensured that the code project compiles fully before i save the template and i save it from within word and not within the IDE. 

    thanks for your time and sugestions doug, but i can see by the lack of other response to my problem that im probably experiencing something that nobody else has and maybe i need a complete machine rebuild and office install and see if it happens after that?

    matt

    Tuesday, April 5, 2011 11:33 AM
  • Strangely, it was only yesterday that I came across a situation where all of the code appeared to have disappeared from a template (I say appeared because it was not really the case).

    The situation was that I had a fifty off page template that when opened was to display a userform that was populated with some default values that were stored as document variables in the template and the population was done by the initialize routine in the userform.

    When I tried to create a new document from the template, the Userform did not appear and also, there appeared to  be no macros in the template (there were in fact 3 of them, the autonew and a couple of others that were not really necessary for the operation of the template, but were used during its creation, one of them being to set the initial values of the document variables)   What turned out to be causing my problem was a difference in spelling of a variable name between the code that was setting the intial values and the code in the userform initialize statement that was retrieving those values, which was "erroring out" because it was trying to set the text property of a control on the userform to the value of a variable that did not exist.  While I was able to produce an error message and locate the error by stepping through the code, when the autonew macro was run when using File>New to create a document from the template, the new document was created, but the userform was not shown and there appeared to be no macros in the template nor in the VBE when accessing the template to which the reference was created.

    Using File>Open however to open the template, all of the code was there so I was able to step through it and fix it.

    That however is the first time since the advent of UserForms in Office 97 (so about 12 or so years now) that I have ever come across that happening and I have never heard of anyone loosing code as you have reported.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Matt Cope" wrote in message news:859b0c1d-e3a1-4655-838a-3075e72461c8@communitybridge.codeplex.com...

    thanks doug

    so if there was a possibillty that it is secuirty settings and an untrusted location issue, how should i go about addressing the problem? im not sure what locations i can set as trusted.  also, i have signed the code with a valid digital signature.

    i have tools to export all of my code modules and i do this regularly, along with keeping the templates in source control, and this certainly helps.  but this problem happens so randomly that i inevitably can lose several hours of work as there is no indication of this problem about to happen until i have closed the template and then reopen it

    possible corruption, yes i think this could be the case but as i mentioned before the templates only contain code and dont have any design elements in them (ie no content and no changes to the styles etc).

    something else that i have noticed about when this happens but i am neither 100% certain this is the case or if it is purely coincidence and would just confuse the issue.....   the problem possibly only happens if i have not ensured that the code project compiles fully before i save the template and i save it from within word and not within the IDE.

    thanks for your time and sugestions doug, but i can see by the lack of other response to my problem that im probably experiencing something that nobody else has and maybe i need a complete machine rebuild and office install and see if it happens after that?

    matt


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Friday, April 8, 2011 10:07 AM
    Tuesday, April 5, 2011 8:14 PM
  • thanks doug

    i will continue to investigate this and maybe one day i will be able to provide the necessary steps to replicate the problem. 

    in the meantime i still have the corrupt dotm file that has no code in nor even a VB project associated with it.  i have no way of analysing this file to find out what may have happened to it to remove the VBProject from it, and i guess it's beyond the scope of this forum for me to be able to send it to you for an MVP (or Microsoft techy) to examine it further?

    also, i have discovered today that even though i can open the template in Word and can type text into the pages, Word somehow dosn't see it as existing or even being open, as running the following code returns nothing.

    Sub GetDocs()
    Dim objD As Document
    For Each objD In Documents
      Debug.Print objD.Name
    Next
    End Sub
    
    

    thnks again

    Matt

    Monday, April 11, 2011 12:02 PM
  • Hi Matt,

     

    The trusted location settings in 2007 are in Office Button>Word Options>Trust Center>Trust Center Settings. This will show where Word is expecting user templates to be. If placed there, they default to trusted. Also take note of the setting on this dialog about whether subfolders are allowed. Other locations can be added.

     

    If your application ever updates itself (new styles, autotext, etc.) you obviously have to store your template in an area where Full Trust and Full Control are allowed.

     

    I wasn’t clear if this problem was occurring just on your development machine or on machines where your add-in has been deployed. It also seems to me that your statement about it potentially occurring if the project isn’t compiled successfully might be a major clue.

     

    I’m neither an MVP or a Microsoft Techy but I’m happy to take a look. You can de-spam and send to, rvmichaels at gclearning dot com.


    Regards
    Monday, April 11, 2011 3:53 PM
  • Hi Matt,

     

    I’m guessing but I think there’s something in your AutoExec procedure that is on occasion generating an exception.  When that happens Word disables the macros because they have interfered with Word’s initialization.  As you are probably aware, an AutoExec procedure will always attempt to run immediately.  However, I have seen that if Word isn’t ready to do what your code wants to perform, problems can occur.  That’s why I avoid AutoExec routines. 

     

    I think it’s safer to duplicate a template’s startup initialization requirements in the ThisDocument events of Document_New or Document_Open.  Alternatively the AutoNew or AutoOpen procedures can also do the job but even there I often put a one second time delay in, just to make sure Word is ready to listen.

     

    In the template you provided I received the File Not Found message. I received it when I tried or open the template as a document, when I attached the template to an existing document, or when I just tried double clicking on the template. I can see the names of the macros in the template, but none execute. And your add-in’s command-bar does display in the Add-in tab on the ribbon.

     

    I did note that your template shows as being in compatibility mode.  I’m not sure that has anything to do with this, but then again maybe …


    Regards
    Tuesday, April 12, 2011 3:24 PM
  • many thanks for this response rich, it certainly is most helpful.

    and yes, you were correct, there is indeed an autoexec procedure firing.  very interesting to note your knowledge about the word startup procedure and the possible failure occuring in my autoexec routine.  i will look towards testing & removing it from the core code and utilise the document_open event instead.

    i can see the macros too, and again none of them run.  word has most definitely stripped them out of the template, along with everything else that is part of the VB Project.

    also worth bearing in mind your note about compatibility mode...  i will check my templates should this happen in future.  i once knew where to find out this information about a word doc but today i can no longer find it! 

    there's so much about under the hood of Word that just seems not to be documented or easily available, probably for good reason, but to have more info or a decent book about it would surely be helpful to developers in need!  if you know of any then would be grateful to suggestions

    thanks again

    regards

    matt

    Thursday, June 9, 2011 1:56 PM