none
After save document, I can not open saved document OpenXML SDK 2.5 RRS feed

  • Question

  • After I save document (object Document open myfile.docx and save it as myfileEdited) Document.SaveAs("myfileEdited.docx"); or I use Close(); when I call 

    using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true)) 
    {
    // ...
    }

    it crash here and I got message 

    "Unhandled Exception: System.IO.IOException: The process cannot access the file 'path to file' because it is being used by another process."


    Monday, July 23, 2018 1:07 PM

Answers

All replies

  • Hello Striebrovlas,

    How do you save the document? What did you do before opening it using OpenXML? Is there any related code.

    Besides, as the error message said, the file should be used by another process. Make sure that the document file is closed or not been accessed by any other process.

    Best Regards,

    Terry


    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 24, 2018 2:08 AM
  • I save document with Document.SaveAs("myfileEdited.docx"); 

    This is what all I do

    using (WordprocessingDocument Document = WordprocessingDocument.Open(doc, false))
    {
        Document.SaveAs("myfileEdited.docx");
        Document.Close();
        using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true)) // ERROR
        {
            // ...
        }
    }
    I tried Document.Dispose(); but it doesn't help even when I do this:
    using (WordprocessingDocument Document = WordprocessingDocument.Open(doc, false))
    {
        Document.SaveAs("myfileEdited.docx");
        Document.Dispose();                
    }
    using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true)) // ERROR
    {
        // ...
    }
    It looks like Document is allways still in memory or in process, even using(){} ends and all Close, Dispose is called.

    Tuesday, July 24, 2018 7:01 PM
  • Hello Striebrovlas,

    I checked members of WordprocessingDocument and did not find the SaveAs method. I'm wondering how did you call the SaveAs function. 

    For avoiding misunderstanding, I would suggest you share a simple project for us to reproduce your issue.

    For sharing file, you could share the file via Cloud Storage, such as One Drive, and then put link address here. Thanks for understanding.

    Best Regards,

    Terry


    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 25, 2018 9:14 AM
  • Hello Terry

    for SaveAs(), I call it when I type Save for document, it show me options, and there was SaveAs. That is all.

    Documentation is from 2013 and now is 2018 I use latest OpenXML SDK 2.5. I mentioned it in my topic name.

    here is source.


    • Edited by Striebrovlas Thursday, July 26, 2018 1:53 PM change link format
    Thursday, July 26, 2018 1:51 PM
  • Hello Striebrovlas,

    Thanks for sharing the project and I could not reproduce your issue with your project.

    After checking, I found that you are not using Version 2.5 but version 2.8.1. To be honestly, I don't the SaveAs method before and failed to find a document for the detail of the method. A clear result is that the myfileEdited.docx is not created via the function.

    If you want to save a modified WordprocessingDocument document, I would suggest you refer to below thread now.

    Save modified WordprocessingDocument to new file

    Best Regard,

    Terry


    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. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, July 27, 2018 10:08 AM
  • But this is not problem for me. It save document (even if program crash) and file myfileEdited.docx is created. Even it can open, but only then, when my program is finished and closed. If my program run and save document while program is running and try open it after save (as is in my source or in example posted above), then it crash for some unknow reason for me.

    Ok, you are right it is version 2.8, and when you start searching SaveAs() for this version, you get

    https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.packaging.openxmlpackage.saveas?view=openxml-2.8.1

    When I was looking for open, manipulate and save docx files, I found OpenXML SDK 2.5 only and then link to the nugetpackage with 2.8. Then my topic name is missleading (as microsoft documentation, because I thinked only 2.5 version is the latest). 




    Friday, July 27, 2018 3:25 PM
  • I try accepted example from topic you post. And I discover, I can not use 2x Open() with "string.docx" string inside it. This crash application when I use Open() after document is opened. If I add stream for first Open, second Document.Open() it open without problem or crash. I am new in C#, but coming from C++, I only guess but it looks like when stream is used, after finish SaveAs, it hold "myfileEdited.docx" (maybe file or only name for file) in memory and I cant open file because it is in use (in memory) even when Document is closed.
    Friday, July 27, 2018 3:52 PM
  • Btw. thanks for link, but that example is not working as expected. Because it save not only new file but original too. This is unwanted effect. But this is working for me:

    static void Main(string[] args)
            {
                byte[] byteArray = File.ReadAllBytes("template.docx");
                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(byteArray, 0, (int)byteArray.Length);
                    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
                    {
                        File.WriteAllBytes("myfileEdited.docx", stream.ToArray());
                    }
    
                    using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true)) // crash on opening saved file
                    {
                        // code for edit file
                        Body b = Document2.MainDocumentPart.Document.Body;
                        Paragraph p = b.AppendChild(new Paragraph());
                        Run r = p.AppendChild(new Run());
                        r.AppendChild(new Text("Maybe there is the light?"));
                        Console.WriteLine("new file saved");
                    }
                }
                System.Diagnostics.Process.Start("myfileEdited.docx"); // file is opened.
            }

    Btw. before this, I found CreateFromTemplate() method and you can open docx file which are copied in to memory and I can easiy with few codes open, manipulate files, but it do errors as I describe it before, when I want open second document for edit.

    using (WordprocessingDocument Document = WordprocessingDocument.CreateFromTemplate("template.docx", true))
                {
                    Body b = Document.MainDocumentPart.Document.Body;
                    Paragraph p = b.AppendChild(new Paragraph());
                    Run r = p.AppendChild(new Run());
                    r.AppendChild(new Text("Maybe there is the light?"));
                    Document.SaveAs("myfileEdited.docx");
                    Console.WriteLine("new file saved");
                }
                System.Diagnostics.Process.Start("myfileEdited.docx"); // can't open because document is in use

    Process.Start("myfileEdited.docx"); try open document, but it throw File is in use and don't open document. But with previous example, document is opened.

    Friday, July 27, 2018 4:14 PM
  • When a Word document is open in the Word application Windows places a "file lock" on it. It's not possible to work with a document anywhere else when a file lock is in-place. Even if the document has been closed, if your other code still has any objects related to that document, such as a Word.Document variable, that will keep the file locked until all these objects have been released and garbage collected. Are you doing that in the other code and forcing garbage collection?

    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Monday, July 30, 2018 5:09 PM
    Moderator
  • I am not sure if I understand. If I do

    string f = "file.docx";
    using (WordprocessingDocument Document = WordprocessingDocument.CreateFromTemplate(f, true))
    {
        // ...
    }

    this mean string f; is related to Word.Document? I am new in C#.

    What all I do, is everything in this topic. Nothing more is inside my code. You can copy/paste code from my latest post with code and it should working (but you must use using System. etc... and OpenXML SDK) as I described.


    Monday, July 30, 2018 8:00 PM
  • The problem is this:

    ================

    After I save document (object Document open myfile.docx and save it as myfileEdited) Document.SaveAs("myfileEdited.docx"); or I use Close();

    =================

    You don't show us the full code for the above. But it's certain that, up through this point the file is locked by the Word application. Therefore, Open XML SDK can't work with it. How long it remains locked will depend on how your INTEROP code is handling the COM objects. If they aren't getting released and garbage collected the file lock could be maintained until that code goes completely out-of-scope. But since you don't provide any details about the INTEROP code, we can only guess...


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    Tuesday, July 31, 2018 8:21 PM
    Moderator
  • I provided full source code with docx files. You can download it and look at it.

    But for you here is full source, both functions are not working due problems I previously described

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Text.RegularExpressions;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Wordprocessing;
    
    namespace test2
    {
        class Program
        {
            static void notWorkingExample1()
            {
                using (WordprocessingDocument Document = WordprocessingDocument.CreateFromTemplate("template.docx", true))
                {
                    Body b = Document.MainDocumentPart.Document.Body;
                    Paragraph p = b.AppendChild(new Paragraph());
                    Run r = p.AppendChild(new Run());
                    r.AppendChild(new Text("Maybe there is the light?"));
                    Document.SaveAs("myfileEdited.docx");
                    Console.WriteLine("new file saved");
                }
                System.Diagnostics.Process.Start("myfileEdited.docx");
            }
    
            static void notWorkingExample2()
            {
                using (WordprocessingDocument Document = WordprocessingDocument.Open("template.docx", true))
                {
                    Document.SaveAs("myfileEdited.docx");
                }
    
                using (WordprocessingDocument Document2 = WordprocessingDocument.Open("myfileEdited.docx", true))
                {
                    // code for edit file
                    Body b = Document2.MainDocumentPart.Document.Body;
                    Paragraph p = b.AppendChild(new Paragraph());
                    Run r = p.AppendChild(new Run());
                    r.AppendChild(new Text("Maybe there is the light?"));
                    Console.WriteLine("new file saved");
                }
                System.Diagnostics.Process.Start("myfileEdited.docx");
            }
    
            static void Main(string[] args)
            {
                notWorkingExample2();
            }
        }
    }
    

    Thursday, August 2, 2018 2:36 PM
  • This question has come up before and been answered on StackOverflow.

    https://stackoverflow.com/questions/49633150/saveas-wordprocessingdocument-open-xml-file-is-locked

    In a nutshell: the file is still open for editing and must be closed. For example using

    wordDoc.SaveAs("myfileEdited.docx").Close();


    Cindy Meister, Office Developer/Word MVP, <a href="http://blogs.msmvps.com/wordmeister"> my blog</a>

    • Marked as answer by Striebrovlas Thursday, August 2, 2018 4:48 PM
    Thursday, August 2, 2018 4:21 PM
    Moderator
  • Thanks, this is working.
    Thursday, August 2, 2018 4:48 PM
  • I had the same problem.

    This works.
    wordDoc.SaveAs("myfileEdited.docx").Close();


    This does not work.
    wordDoc.SaveAs("myfileEdited.docx");
    wordDoc.Close();

    I font know why or how but it just does :)

    Go figure...

    Br Nig


    • Edited by Krellon Wednesday, September 26, 2018 2:39 PM
    Wednesday, September 26, 2018 2:38 PM