none
COM Exception- EXCEL 2007 -This command is not allowed because the document is locked. at Microsoft.Office.Core._CustomXMLPart.Delete() RRS feed

  • Question

  • Hi

    We have developed Office project (Excel 2010 Workbook) using VSTO. It works fine on Win7/Win 2008R2 with Office 2010/2013.

    When we tested it on Office 2007 it gives this error:

    System.Runtime.InteropServices.COMException (0xE0041818): This command is not allowed because the document is locked.
       at Microsoft.Office.Core._CustomXMLPart.Delete()

    It comes at a place where we try to Add/Delete CustomXMLPart to CustomXML in Excel.

    Logic:

    When we open excel for the first time we write current time to CustomXML and then excel is saved and closed.

    When excel document is opened again it fails as we delete existing CustomXML and create again to write current time.

    But logic works fine for Office 2010/2013.

    Another thing I tested was after opening first time and then after saving it when I try to open same excel file in 'Open XML Package Editor Power Tool for VS2010' it says File contains corrupted data.

    Also after renaming .xlsx to .zip it says “The compressed (zip) folder is invalid or corrupted".

    I created simple prototype project to test same functionality but that works fine with same code.

    After reading some links, I tried changing Microsoft.Office.Interop.Excel & Office assemblies to v12 from v14 and Embed Interop Assemblies property is set to true but still that did not work. Then I also installed 2007 PIA. I get same error.

    After saving file in Office 2007 it gets corrupt (I also see file size increases by 4kb when compared to excel 2010).

    Code:

             WorkbookOpenDateTime = new DateTime();
                WorkbookOpenDateTime = DateTime.Now;
                
                XNamespace nameSpace = Properties.Resources.CustomXMLNamespace;

                Office.CustomXMLParts checkCustomXML = CustomXMLParts.SelectByNamespace(Properties.Resources.CustomXMLNamespace);

                if (checkCustomXML.Count == 1)
                {
    //SECOND TIME and SO IT RUNS THIS CODE:
                    XDocument document = XDocument.Parse(checkCustomXML[1].XML);
                    XElement element = document.Element(nameSpace + Properties.Resources.XMLRoot)
                                               .Element(nameSpace + Properties.Resources.XMLWorkBookOpen);
                    element.Value = WorkbookOpenDateTime.ToString();
    //HERE checkCustomXML[1].XML & document variable gets expected value
                    checkCustomXML[one].Delete();   //<- gives error here
                    CustomXMLParts.Add(document.ToString());
                }
                else
                {
    //FIRST TIME WORKBOOK OPEN IT RUNS THIS CODE:
                    XDocument schDocument = new XDocument(
                                                    new XElement(nameSpace + Properties.Resources.XMLRoot,
                                                        new XElement(nameSpace + Properties.Resources.XMLWorkBookOpenMSP,
                                                                 WorkbookOpenDateTime.ToString())
                                                                )
                                                               );
                    
                    CustomXMLParts.Add(schDocument.ToString(), missing);
    //IF I DELETE HERE AND ADD IT again it WORKS WITHOUT PROBLEM
                }

    NOW, I added one button in ribbon control and tried to change same value on click event of it without saving workbook but there also I get same error. This time I did not save Workbook. Logic: Opened Workbook so it wrote value to CustomXML and then clicked button but now it gave error there. I added messagebox to get value of checkCustomXML[1].XML before delete statement which displays correct XML data.

    Anyone knows about this please let us know.

    Thanks,

    Harvinder

    Wednesday, March 19, 2014 4:01 PM

Answers

  • I found the problem, it is due to WorkBook Protection.

    Before Add/Delete() to CustomXML, unprotect the workbook then it works fine in Excel 2007 also.

    Thursday, March 20, 2014 1:34 PM

All replies

  • I found the problem, it is due to WorkBook Protection.

    Before Add/Delete() to CustomXML, unprotect the workbook then it works fine in Excel 2007 also.

    Thursday, March 20, 2014 1:34 PM
  • Hi,

    If your issue was resolved, I will close this case.

    Thank you for sharing your solution and experience.

    Regards,

    Marvin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, March 21, 2014 1:21 AM