none
SaveAs to docx results in a blank word document

    Question

  • I'm creating a Word 2007 AddIn using VSTO with Visual Studio 2008.  I have a requirement where I need to create a copy of the currrent document so that we can do some additional processing on the copy.  We do not want the newly created copy to become active in the word environment. 

     

    The Document.SaveAs() method seems to work as intended.  It creates a docx file, and does not make it the active document.  However. no matter what I do, the resulting docx is a blank document when I open it.  I've tried many variations of SaveAs, with the same results.

     

    Am I missing something with the SaveAs functionality?  Is there another method I can use to acheive the desired end result?  We don't want to force the user to save the original document first (ie. Save the document, use file system to make a copy , etc.).

     

    Thanks!

    Thursday, March 20, 2008 9:41 PM

Answers

  • Hi Stuart

     

     StuartCox wrote:

    Narrowed down the SaveAs problem a bit.  If the SaveFormsData parameter is set to True, you get the behaviour I was describing.  Word saves a blank document with the new file name, without opening it, as I described earlier.

     

    If you set the SaveFormsData parameter to False, Word saves a copy of your document, and opens it in the active window pane.  Not exactly what I was looking for, but Word is now at least doing what I expected it would do with a SaveAs. 

    Ah, that parameter setting explains what you were getting. I admit I was a bit perplexed until you mentioned it <g> This is used when Word Form Fields (part of the "legacy" listing of fields in the Developer tab) are in the document. With this option activated, only the data stored in the form fields is saved - to a plain text file BTW - as a separate document. Since you aren't using form fields, the result was understandably "empty".

     

    You might try the OpenXMLDeveloper.org site. Someone there may already have accomplished the kind of document building from a loaded file that you have in mind. With copy/paste or the XML property of the Document.Content object you'd certainly get everything the user sees in the document. To get any XML you've added to the document package (including document properties and some building block stuff) you can use the CustomXMLParts object. The XML properties can both be streamed.

     

    (The difference with streaming the XML and building a file the way I mentioned before is that you can put the XML into a "Package", making it a valid 2007 document. Just putting it into a text file creates a 2003 document.)

     

    Please note that in-depth discussions of OpenXML are off-topic in this, the VSTO forum. The better place to pursue the topic is OpenXMLDeveloper.org.

    Sunday, March 23, 2008 10:18 AM
    Moderator

All replies

  • Mmmmm. I'd like to recap what you've laid out, with some comments, to make sure we're on the same wave-length, here...

     

    The user has opened or created a document. You want to make a copy of that document and you use the SaveAs method. Then you state, "We do not want the newly created copy to become active in the word environment." In fact, if you use SaveAs the document you saved will be the active document in the Word environment. This is the way Word is designed to function. It can only not be the active document if you immediately close it after saving. But the original document will also not be present and cannot be present because you have never saved it ("We don't want to force the user to save the original document first".)

     

    So I'm having difficulty understanding exactly what you mean with: "The Document.SaveAs() method seems to work as intended.  It creates a docx file, and does not make it the active document." What you state here simply is not possible unless you've closed the document after saving...

     

    Under most circumstances, you'd probably get the result you want by writing the Document.Content.XML to a plain text file with the extension .doc. Word 2007 should recognize this as a Word 2003 XML document and open it with no problem.

     

    If the user may be working with things specific to Word 2007 (content controls come to mind) then you'd have to reconstruct the file in the Word 2007 OpenXML file format. More information on that file format can be found at OpenXMLDeveloper.org.

     

    Alternately, you could copy the entire document on the screen, paste it to a new document, then use SaveAs on that and close it.

    Friday, March 21, 2008 9:22 AM
    Moderator
  • Thanks for your quick response!

     

    I was under the impression that the SaveAs() method would save the document and make the newly saved document become active in the Word environment, as you described.  That's the way it works from the Word menu, so that's what I was expecting that.  I actually tried the SaveAs() as a lark, just to be sure that was correct because it does not meet our requirement.

     

    What I found, however, is that after calling SaveAs (to docx), the new document copy did not become the active document in Word.  The original document (whether an unsaved Document1 or another test document loaded from disk) stays active, and a new docx file is created on disk with the name specified, unopened and wiating there on disk for me.  I thought, "Bingo! Exaclty what I want!"  But when I open the new document, it's a blank document.

     

    I eed the copy to be a docx file.  We have a lot of Word 2007 functionality in the document that we need to preserve in the copy (Content controls, custom xml nodes in the document, custom xml parts attached to the package, schemas, etc), created using the Word API with a Visual Studio 2008 AddIn.  So it's not as simple as copying the contents of a generic word document to a new one, I need to get all the other goodies in the resulting copy as well. 

     

    I'm looking for a simple route to create a copy of the document in docx format.  The Packaging API's would work nicely if I could only pass the current Word document in as a stream, or get a bye array for it that I could convert to a stream, but I don't see those exposed anywhere in the Interop API. It would be nice to have a simple way to transfer or translate a Word.Document object (Interop) into a OpenXML package object.  I'm sure we can construct a package using the packaging API, but that sounds time consuming and error prone, especially when what I want is an exact copy of what I already have, right under my nose!.  I was hoping to find a faster/simpler approach to make the copy.

     

    On the SaveAs(), it feels like I'm very close, but maybe not.  The behavior I'm seeing seems quite odd though, in any case.

     

    Friday, March 21, 2008 1:12 PM
  • Narrowed down the SaveAs problem a bit.  If the SaveFormsData parameter is set to True, you get the behaviour I was describing.  Word saves a blank document with the new file name, without opening it, as I described earlier.

     

    If you set the SaveFormsData parameter to False, Word saves a copy of your document, and opens it in the active window pane.  Not exactly what I was looking for, but Word is now at least doing what I expected it would do with a SaveAs. 

     

    Thanks!

     

    Friday, March 21, 2008 1:58 PM
  • Hi Stuart

     

     StuartCox wrote:

    Narrowed down the SaveAs problem a bit.  If the SaveFormsData parameter is set to True, you get the behaviour I was describing.  Word saves a blank document with the new file name, without opening it, as I described earlier.

     

    If you set the SaveFormsData parameter to False, Word saves a copy of your document, and opens it in the active window pane.  Not exactly what I was looking for, but Word is now at least doing what I expected it would do with a SaveAs. 

    Ah, that parameter setting explains what you were getting. I admit I was a bit perplexed until you mentioned it <g> This is used when Word Form Fields (part of the "legacy" listing of fields in the Developer tab) are in the document. With this option activated, only the data stored in the form fields is saved - to a plain text file BTW - as a separate document. Since you aren't using form fields, the result was understandably "empty".

     

    You might try the OpenXMLDeveloper.org site. Someone there may already have accomplished the kind of document building from a loaded file that you have in mind. With copy/paste or the XML property of the Document.Content object you'd certainly get everything the user sees in the document. To get any XML you've added to the document package (including document properties and some building block stuff) you can use the CustomXMLParts object. The XML properties can both be streamed.

     

    (The difference with streaming the XML and building a file the way I mentioned before is that you can put the XML into a "Package", making it a valid 2007 document. Just putting it into a text file creates a 2003 document.)

     

    Please note that in-depth discussions of OpenXML are off-topic in this, the VSTO forum. The better place to pursue the topic is OpenXMLDeveloper.org.

    Sunday, March 23, 2008 10:18 AM
    Moderator