Welcome to OpenXML Developer Sign in | Join | Help

Failure to create package of opened .docx

Last post 07-28-2008, 12:12 AM by Sheela E N. 2 replies.
Sort Posts: Previous Next
  •  07-17-2008, 12:13 PM 3478

    Failure to create package of opened .docx

    Hi

    I'm developing a Word add-in and I'm trying to access the xml-files of the .docx file which is currently opened.

    Here's the essential parts of my code:

    private Word.Document doc;       
    ...
    String path = doc.FullName;
    String documentPathInPackage = "word\\document.xml";
    Uri partUri = Packaging.PackUriHelper.CreatePartUri(new Uri(documentPathInPackage, UriKind.Relative));

    Packaging.Package docPackage = Packaging.Package.Open(path, FileMode.Open, FileAccess.ReadWrite);           

    It's the last line that fails, the debugger says "The file is currently being used by another process", which is true.

    So my question is how can I do this in a neat way? Is it possible to unlock the file from the process using it?

    Thanks in advance.

               
  •  07-23-2008, 5:46 AM 3500 in reply to 3478

    Re: Failure to create package of opened .docx

    Hi all!

    Just wanted to say that I solved the problem by opening the file in read-only mode, which is not a perfect solution.

    Correct line is:
    Packaging.Package docPackage = Packaging.Package.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
  •  07-28-2008, 12:12 AM 3505 in reply to 3500

    Re: Failure to create package of opened .docx

    Hi,

    First you open the package as

    pack = Package.Open(@"file path", FileMode.Open, FileAccess.ReadWrite);

    Then create the main document part as

    PackagePart mainPart = pack.CreatePart(uriHeaderFooter, "content type");

    Sheela

View as RSS news feed in XML