4

In our WPF application, we must create a Word document and open it automatically.

Today we ask the user to specify the file name and location BEFORE we create the document and them open the file using the Process.Start method.

Now, I want to create the Word document in memory stream, open it and eventually, when the user decides to save the document in Microsoft Word, he will have to specify the location and name. So I will not be using the SaveFileDialog in my app.

This is similar to when you start Microsoft Word. A default document is created and when you click save, you will guided to the "save as" function.

How can I do that?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Igor Kondrasovas
  • 1,479
  • 4
  • 19
  • 36
  • 1
    There are so many examples online on how to create a word doc using InterOp as well as HttpContext I do this all the time in my web applications currently also look into using `OpenXml` if you are not familiar with Microsoft.Interop – MethodMan Nov 25 '15 at 20:11
  • @MethodMan My question is not about how to create the contents of a Word document, but how to get a memory stream created document and launch it in Word without a phisical file path. I am not using interop by the way. – Igor Kondrasovas Nov 25 '15 at 20:14
  • http://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/09/creating-a-new-microsoft-word-document-from-a-template-using-openxml.aspx – MethodMan Nov 25 '15 at 20:22
  • http://stackoverflow.com/questions/16194646/create-word-document-with-open-xml – MethodMan Nov 25 '15 at 20:27

2 Answers2

4

I don't think you can do this purely on the memory stream. However, I would save the memory stream to a temporary file, by saving to the Temp folder or AppData\temp or something like that with a randomly-generated name, and then mark that file as read-only. Then open word on that file (with System.Diagnostics.Process or however you are doing it), and since it is read-only, it will ask the user to save changes when they exit.

sovemp
  • 1,402
  • 1
  • 13
  • 31
  • Are you saying that if I mark the file as read-only it will force Microsoft Word to ask user to "Save As..." with another name instead of saving directly to the temporary path I created? – Igor Kondrasovas Nov 25 '15 at 22:17
  • Yes exactly. I think this is similar to how web browsers do it if I'm not mistaken. – sovemp Nov 26 '15 at 00:23
  • 1
    Building on sovemp's content: It's not possible to "stream" content into the Word application. Closest you can get requires some kind of "interop" interaction, either pasting from the Clipboard, using InsertFile or Range.XML in order to put content into an already open Word document. There's no way to create a document from a stream - Word simply is not built to do that. – Cindy Meister Nov 26 '15 at 16:22
3

Just programmatically create a new Word document using the standard Microsoft.Interop.Word .Net namespace:

Note that you might need to install MS-Office to do this.

Note, too, that your application can display it's own "Save As" dialog (or, for that matter, could just use a hard-coded path) independent of Word. Your program chooses the path - and your program writes the Word document object if/when it's ready.

Finally, here's an alternative, open source library, DocX:

ADDENDUM:

Thank you for your clarification:

  1. You're using the OpenXML SDK (as an alternative to .Net Interop or DocX libraries I mentioned above). You've already created an in-memory document object.

  2. Now you want the user to be able to open the document object in Word (presumably to review it), before saving it (to a filename of his/her own choosing).

  3. You can't (easily) do that :)

  4. One option, suggested by sovemp above:

    a. OpenXML writes to a temp file (so it can be opened in Word)

    b. Make the temp file read-only (to force the user to do an explicit "Save As")

    c. Use .Net Process.Start() to invoke MSWord (specifying your temp file in your command line).

  5. Another option might be to use a "Preview Handler":

  6. Still another option might be to serve the memory stream in a web browser, using HTTP ContentType "application/msword" (for example)

  7. Finally, if all you want is for the user to specify a filename (if "preview" isn't essential), you can always pop up your own "Save as" dialog and write document to disk directly from OpenXML.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • I am not using Interop. I want to get my memory stream created document and open it in word in a way that if the user clicks save (in Microsoft Word) the Word file save as will be shown. – Igor Kondrasovas Nov 25 '15 at 20:17
  • Exactly how are you using this "Memory Stream"? *You don't need a memory stream to create a doc file* - you just need an "object". An in-memory object. Created by Interop, or by DocX, for example. Q: So what's it for? How is it created? What is your "use case" for requiring a memory stream? Please clarify. – paulsm4 Nov 25 '15 at 20:19
  • My application creates a Word document using OpenXML. The file content is not my question here. This is working great. I want to launch this file in Microsoft Word. When the user clicks "Save" on Microsoft Word, I want that word asks where to save the file. So I suspect I cannot open this document form a file stream. – Igor Kondrasovas Nov 25 '15 at 20:23