none
Documents.Open(...) throws "Command failed" error in Word 2010 when User Templates are stored on a network share RRS feed

  • Question

  • Hi,

    I have a C#  (.Net 2.0) desktop application that uses COM interop to automate Word. The application was created a few years ago using the Office XP automation interface and works with Word 2002, 2003 and Word 2007, but not with Word 2010 under certain conditions.

    Our clients keep their Office User Templates on a network share and configure Word to use that location( Options | Advanced | File locations). I've reproduced the problem in a simple test app. To reproduce in Word 2010, configure your Word User Templates to point to a network location. In the Trust Center set Word to trust network locations and add your new location (initially I thought that this would be the fix). The following code breaks, but if a sufficient delay is introduced it works. I didn't experience the problem using a local User template location. The error raised on the Documents.Open call is

    System.Runtime.InteropServices.COMException (0x800A1066): Command failed

    Is this a Word bug/setting, an interop setting/problem, or is there a way for me to receive an event of some sort when the Application object is in an intialized and working state? - Adding sleep statements in my code is not a good solution - somewhere out in my client base will be a machine where that sleep value wont work.

    Thanks for your time

     Bruce

     

    object filename = @"C:\temp\worddoc.tmp";

    object missing = System.Reflection.Missing.Value;

    _Application app = new Microsoft.Office.Interop.Word.Application();

    app.Visible = true;

    //  Thread.Sleep(1000);

    app.Documents.Open(ref filename, ref missing, ref missing, ref missing, ref missing,

                       ref missing, ref missing, ref missing, ref missing, ref missing,

                       ref missing, ref missing, ref missing, ref missing, ref missing);

               

    //app.Quit(ref missing, ref missing, ref missing);

    Wednesday, March 9, 2011 6:39 AM

Answers

  • Hello Bruce,

    You ask whether there is a way to know when the Word application is loaded. The call to

    _Application
    app = new Microsoft.Office.Interop.Word.Application();

    is synchronous. Your application doesn't move on to the next step until you have a valid instance of Word.applicaiton instantiated and ready for the next call.

    Office 2010 is different in that it has new security aspects. To give you some perspective about documents that are on a server, here is a snippet of text from another Forum post:
    "- Word 2010 automation document open fails when template directories ...
    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/a6b1cfc2-1de0-46e0-ae9b-b91acb158ffe"
    With Macro Security set to "Disable with Notification," Word 2010 users cannot enable macro security for DOCX documents they've created based on DOT nor DOTM files, nor DOC files based on DOTM files. If they create a DOC document from a DOT template, then they will be prompted and can enable macros.
    Word 2007 doesn't have this problem; it lets users enable macros even if the base template is on the server, and whether or not it is DOT or DOTM, DOC or DOCX.
    This is a significant problem for users that keep macro-enabled templates in the Workgroup Templates path.

    Where your templates may not have Macros, there is undoubtely something about the document that encounters one or more of the security considerations. When there is no problem if the code waits you can colclude that the document did confirm to the Word 2010 secrity boundaries.

    So, use of the programmatic 'Wait.." is a legitimate solution. The Wait argument is in milliseconds no matter the clock speed of the CPU. Your application runs on Microsoft Windows, and that operating system understands the call to 'wait' on every version.

    Considering the time to load across the network, any client would report the problem when the version of Word is earlier than 2010. The implication is that the template has been passed to the system running Word, and the delay is probably due to the time required for Word's new security function(s) to process the document.

    The problem you're having is because of the design of Office 2010 applications.

    I hope this information is helpful. If this misses the mark for you then because of its complexity your question falls into the paid support category which requires a more in-depth level of support.  If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged. Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    • Proposed as answer by cjatmsModerator Tuesday, March 22, 2011 9:08 PM
    • Marked as answer by BruceH Wednesday, March 23, 2011 11:34 AM
    Tuesday, March 15, 2011 5:18 PM
    Moderator

All replies

  • Hello Bruce,

    Thanks for posting. For this issue, I think we could do more test to open a docx file from C:\temp path, and see if this issue also happens. About more information about Trusted location, please see these resources, and try the steps again.

    What is the secret to adding a Trusted Location to Word:
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/a6826a31-abb5-417d-b2dc-cf7d39f6f0b6.

    Plan Trusted Locations settings for Office 2010:
    http://msdn.microsoft.com/en-us/library/cc179039.aspx.

    Also, would you confirm one thing for us? Does this word automation run on a server? For the topic about server-side office automation, please check this KB article: http://support.microsoft.com/kb/257757.

    We are looking forward to your reply. Have a nice day.


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, March 10, 2011 3:12 AM
  • Hi Bessie,

    Thanks for the info. I read the articles. From what I read I think that I have the right settings, maybe you can confirm this. I have tried loading a file with .doc and docx extension with the same result. As for your last question, my application is a desktop application, so it's not run on a server.

    Our users have a typical word 2010 installation. They want to share default word settings (styles etc) so they've stored the normal.dotm (and other ) files on a network share and set the location as the User Template location in ( Options | Advanced | File locations). They've also set the location as a trusted location and enabled trusted locations not on the PC in the Trust Center. The documents are sometimes also stored on a network location, but the problem occures even if the document is local.

    With these settings the Documents.Open(...) commnad fails, but works with delay between the object initialization and the .Open call.  If I remove the settings in the Trust Center I get the same results. So maybe I'm using the wrong settings.

    Regards

     Bruce

     

     

     

    Monday, March 14, 2011 11:56 AM
  • Hello Bruce,

    You ask whether there is a way to know when the Word application is loaded. The call to

    _Application
    app = new Microsoft.Office.Interop.Word.Application();

    is synchronous. Your application doesn't move on to the next step until you have a valid instance of Word.applicaiton instantiated and ready for the next call.

    Office 2010 is different in that it has new security aspects. To give you some perspective about documents that are on a server, here is a snippet of text from another Forum post:
    "- Word 2010 automation document open fails when template directories ...
    http://social.msdn.microsoft.com/Forums/en-US/worddev/thread/a6b1cfc2-1de0-46e0-ae9b-b91acb158ffe"
    With Macro Security set to "Disable with Notification," Word 2010 users cannot enable macro security for DOCX documents they've created based on DOT nor DOTM files, nor DOC files based on DOTM files. If they create a DOC document from a DOT template, then they will be prompted and can enable macros.
    Word 2007 doesn't have this problem; it lets users enable macros even if the base template is on the server, and whether or not it is DOT or DOTM, DOC or DOCX.
    This is a significant problem for users that keep macro-enabled templates in the Workgroup Templates path.

    Where your templates may not have Macros, there is undoubtely something about the document that encounters one or more of the security considerations. When there is no problem if the code waits you can colclude that the document did confirm to the Word 2010 secrity boundaries.

    So, use of the programmatic 'Wait.." is a legitimate solution. The Wait argument is in milliseconds no matter the clock speed of the CPU. Your application runs on Microsoft Windows, and that operating system understands the call to 'wait' on every version.

    Considering the time to load across the network, any client would report the problem when the version of Word is earlier than 2010. The implication is that the template has been passed to the system running Word, and the delay is probably due to the time required for Word's new security function(s) to process the document.

    The problem you're having is because of the design of Office 2010 applications.

    I hope this information is helpful. If this misses the mark for you then because of its complexity your question falls into the paid support category which requires a more in-depth level of support.  If the support engineer determines that the issue is the result of a bug the service request will be a no-charge case and you won't be charged. Please visit the below link to see the various paid support options that are available to better meet your needs. http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone

    • Proposed as answer by cjatmsModerator Tuesday, March 22, 2011 9:08 PM
    • Marked as answer by BruceH Wednesday, March 23, 2011 11:34 AM
    Tuesday, March 15, 2011 5:18 PM
    Moderator
  • Thanks for the feedback.

    While I understand the complexities of Office, I don't buy the arguement that waiting is a great solution, nor that my senario is complex. I put in a 1000ms wait and still experienced the the problem (thanks to a scheduled PC backup that happend to be running while a conducted a test. Obviously it was slowing down Word). There should be some way for me to determine that Office is ready for further calls, OR, further calls or the creatation call should block until the initialization is done.  The template files I used were the Word default ones.ie I didnt add macros etc, all I did was move them to a network location. 

    I have raised the paid support option as an alternative to us developing a workaround. I can see the woraround getting rather complex. Detect if Word 2010, Get User Template and Workgroup Templates locations ( I think that these are the only 2 settings that cause the problem), check if either is a network location, add a predetermined amount of time, try call, if call fails assume that its the security init problem, wait some more, try again a couple of times, if the call still fails a few times assume the 'command failed' error is something else and let the exception bubble up the stack.

    Regards

     Bruce

    Wednesday, March 23, 2011 11:05 AM
  • Hi Bruce,

    As a workaround would it be possible for your app to save the document with just the norrmal template attached and a reference to the necessary custom template path in a document variable?

    On document open your app reads the variable and attaches the appropriate template and on close or save it strips it.

    Just a thought :-)


    Regards
    Wednesday, March 23, 2011 2:02 PM
  • We experienced a similar problem and found that applying KB2553912 should fix the issue.

    http://support.microsoft.com/kb/2553912

    Please visit http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2553912&kbln=en-us to download the hotfix.

    Thursday, August 18, 2011 10:22 PM
  • i fix this error on windows 7 and windows 2008 creating a folder

    create a folder named "Deskrop" on this location

     (Windows 64 bits)

    C:\Windows\SysWOW64\config\systemprofile\

     (WIndows 32 Bits)

    C:\Windows\System32\config\systemprofile\

    Monday, August 22, 2011 8:21 PM
  • Sebastian,

    I can't believe it!  You're right.  At least, it fixes my 2010 automation problem, where the the doc open just didn't happen at all.  I have found the wait approach useful in the past on 2007, but it seemed to make no odds to this 2010 problem. 

    Thanks a million.  I  have spent so long over the last 10 years working on these automation issues, I really don't feel the need to spend more time.

    Of course, the folder name is Desktop not Deskrop.

     

    Mark

    Saturday, August 27, 2011 9:33 PM
  • Alike you there are several regular users of MS word application who manage with several unknown error tags while working with word. The problem that you are facing can be solved using third party MS word recovery software. Stellar phoenix word recovery software is an effective and efficient way to recover lost contents of word files and also to solve general word file errors.
    Tuesday, January 24, 2012 6:51 AM