none
"custom.dotm template file is locked for editing by user" error appears while opening word docx file using C# code RRS feed

  • Question

  • Hi,

    I have one custom template file located in "C:\Reports\" and I am creating new docx word file inside that folder by using "Custom.dotm" template using my C# code. I am facing one issue while creating another file or appending existing or viewing another docx file which is using the same template. Suppose I open one word file it opens properly but when I tried to open the second word file I am getting the error message box having following description:

     "Custom.dotm is locked for editing by User.

    Do you want to:

    1) Open a Read Only copy.

    2) Create a local copy and merge the changes later.

    3) Receive notification when original copy is available."

    But when I tried to open those files directly by double clicking..it opens without any issue, I dont know how winword.exe is able to open multiple files which belongs to same template...

    Please find the following snip of code.

     

    private IContainer components;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.OpenFileDialog openFileDialog1;
            private System.Windows.Forms.MainMenu mainMenu1;
            private System.Windows.Forms.MenuItem menuItem1;
            private System.Windows.Forms.MenuItem menuItem2;
            private Button button3;

            private Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            public Form1()
            {
               
                InitializeComponent();                 
            }

                   
            private void button1_Click(object sender, System.EventArgs e)
            {

                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {                               
                    object fileName = openFileDialog1.FileName;
                    object readOnly = false;
                    object isVisible = true;
                    object missing = System.Reflection.Missing.Value;

                   
                    try
                    {
                        WordApp.Visible = true;
                    }
                    catch (Exception)
                    {
                        WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                        WordApp.Visible = true;
                    }

                    Microsoft.Office.Interop.Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing,ref readOnly,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref isVisible,ref missing,ref missing,ref missing, ref missing); //while opening 2nd word docx file this command showing the meesage box

                    aDoc.Activate();              

                }
           
            }      
        }

    Please help me out from this issue.

    Note: I am using word2007

    Thanks & regards,

    Monday, January 31, 2011 7:20 AM

Answers

  • <<Actually whenever I am opening a word file it opens the template file too with "$~ustom.dotm". So this means the file is in used. but the same thing I am trying to do outside the test application it works...means if I create 10 docx file using the "Custom.dotm" template and if I try to open those 10 files I am able to open those files.>>

    Yes, I understood that. And I'm pretty sure the problem comes from how you're managing the Word Application in your code. I'm following the logic of how you expect your application to work (the user interaction part), but I get the impression the user may use the WinForm repeatedly, without closing it between uses?

    In that case:

    1. Declare the Word Application field at the class level, but do NOT instantiate it at this point, only set it to null. (As I said previously)

    2. Use the test for null in your code. If it's null, instantiate to a new Word.Application.

    3. Don't do anything in the button Click event to release the WordApp - this will let your code continue to use the same instance (which should solve the issue). Instead, you need to release WordApp when the user form closes. At that point:

    WordApp = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

    OR, if you want to pick up a running instance of the Word application you can use GetActiveObject().


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Rohit_gates Monday, January 31, 2011 10:31 AM
    Monday, January 31, 2011 8:26 AM
    Moderator

All replies

  • Hi Rohit

    You're doing the following twice in your code, once at the class level and again in the try...catch block of your button code:

       private Microsoft.Office.Interop.Word.ApplicationClass WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

    This is probably causing problems. If you need WordApp at the class level, then declare it and set it to null - don't create the Word.Application with the declaration.

    In your code, test if (WordApp==null). If it is, then instantiate WordApp by creating a new instance.

    You also want to be careful to correctly release WordApp, so that you don't leave orphaned instances in memory. I can't tell whether you've left that out of your code, but if what you show us is your entire WinForm app, then that part is certainly missing and is likely what's causing the issues you're experiencing.


    Cindy Meister, VSTO/Word MVP
    Monday, January 31, 2011 7:49 AM
    Moderator
  • thanks for the reply.

    If I make wordApp object to "null" after view/append/create word docx and creating the new object of wordApp if its null then I am getting the message box "locked for editing". And suppose if I not make the object to "null", so before opening docx file i am getting "RPC Server error" near "wordApp.visible = true", so i am catching that exception and re-instantiating the object, so this works fine for everything, but it fails only in one case .... suppose if I open word docx file which having the same custom template outside my test application and then I am executing my test application and trying to open other docx having the same template..so at the first instance I am getting the same message box "locked for editing.."

    And doing MarshalRelaseComObject also not helping me.

    Actually whenever I am opening a word file it opens the template file too with "$~ustom.dotm". So this means the file is in used. but the same thing I am trying to do outside the test application it works...means if I create 10 docx file using the "Custom.dotm" template and if I try to open those 10 files I am able to open those files.

    Monday, January 31, 2011 8:06 AM
  • <<Actually whenever I am opening a word file it opens the template file too with "$~ustom.dotm". So this means the file is in used. but the same thing I am trying to do outside the test application it works...means if I create 10 docx file using the "Custom.dotm" template and if I try to open those 10 files I am able to open those files.>>

    Yes, I understood that. And I'm pretty sure the problem comes from how you're managing the Word Application in your code. I'm following the logic of how you expect your application to work (the user interaction part), but I get the impression the user may use the WinForm repeatedly, without closing it between uses?

    In that case:

    1. Declare the Word Application field at the class level, but do NOT instantiate it at this point, only set it to null. (As I said previously)

    2. Use the test for null in your code. If it's null, instantiate to a new Word.Application.

    3. Don't do anything in the button Click event to release the WordApp - this will let your code continue to use the same instance (which should solve the issue). Instead, you need to release WordApp when the user form closes. At that point:

    WordApp = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

    OR, if you want to pick up a running instance of the Word application you can use GetActiveObject().


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Rohit_gates Monday, January 31, 2011 10:31 AM
    Monday, January 31, 2011 8:26 AM
    Moderator
  • Thanks for the help.

     

    I will try the solution and let you know if it working or not.

     

    thanks

    Monday, January 31, 2011 9:02 AM
  • Thanks for the imp points. The above mentioned notes resolved the issues.
    Monday, January 31, 2011 10:30 AM
  • try this, it will remove the message

    "SampleWPD.wpd is locked for editing by User.

    Do you want to:

    1) Open a Read Only copy.

    2) Create a local copy and merge the changes later.

    3) Receive notification when original copy is available."

    -----------------------------------------------------------------
    object oMissing = System.Reflection.Missing.Value;
    object oTrue = true;
    object oFalse = false;
    object sTempTextFile = @”C:\Doc_Vitals\Doc_VitalsText.txt”;
    object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatUnicodeText;
     
    Microsoft.Office.Interop.Word.Application ap = new Microsoft.Office.Interop.Word.Application();
    ap.Visible = false;
     
    Document doc = ap.Documents.Open(sFileName,
    ref oFalse, ref oTrue, ref oFalse,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
    ref oMissing, ref oMissing, ref oMissing, ref oFalse,
    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
     
    doc.Activate();
    doc.SaveAs(ref sTempTextFile, ref FileFormat);
    ap.Quit(ref oMissing, ref oMissing, ref oMissing);

    zeshan sadiq
    Thursday, June 30, 2011 3:17 PM