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?
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.
Just programmatically create a new Word document using the standard Microsoft.Interop.Word .Net namespace:
How to: Programmatically Create New Documents
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:
http://www.codeproject.com/Articles/660478/Csharp-Create-and-Manipulate-Word-Documents-Progra
How to: Open a word processing document from a stream (Open XML SDK)
ADDENDUM:
Thank you for your clarification:
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.
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).
You can't (easily) do that :)
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).
Another option might be to use a "Preview Handler":
http://www.codeproject.com/Articles/25465/Using-Vista-Preview-Handlers-in-a-WPF-Application
https://previewhandlers.codeplex.com/
Still another option might be to serve the memory stream in a web browser, using HTTP ContentType "application/msword" (for example)
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.
Related
I am trying to make my own application for Microsoft Office Word DRM options.
like: Read only, Print, Encrypted, Watermark.
Details:
I made MS Word add-in for DRM options.
I will handle the printing and readonly permission by using Application_DocumentBeforePrint and Application_DocumentOpen.
But,
I want help for these one.
My file is in encrypted format. So, when I was trying to open that file (On double click), it gives an error for content is not proper.
So, I want to decrypt file on click and then open.
At a time of close or Exit file will encrypted again.
The issue is that by the time Word opens your document it is already invalid. To Word it is just a bunch of invalid bytes, which in your case, is the encrypted document.
You could create a new file extension and register that type with a new shell application (that your write) that performs the decryption and uses Word to open the document. Handling the save (and encryption) would be triggered by your AddIn.
I have .docx files in a sharepoint library. When I open file by the link, it opens in read-only mode
BUT
I can click "Enable edit", edit this document and save it to the sharepoint library.
Can I set only "read-only" permissions for this file?
How can I use DispEx for this?
I think you can set the library permissions to read only, so the user can pull a word doc from there, but not write to the library. As far as a word doc itself goes, I don't think you can keep that in read only. Once a user downloads a file, they can save it and edit it locally.. unless the word document is initially uploaded with some sort or protection. Case in point, open Word (2010+), choose File, and choose Info. There are various options in the Permissions section that should help you out.
In my application, we have a requirement like opening a folder browser dialog on clicking an icon in the word document, and the user will be allowed to add some attachments through that. The word document we generate is a XML based word document and is opened through Microsoft.Office.Interop.Word.ApplicationClass.Is it possible to open a folder browser dialog?
Can we handle this with any event of the document? Any suggestions or inputs would be very helpful.
I think so, just create an instance of OpenFileDialog class and use his ShowDialog method
http://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx
Beware I had some problems in the past with this class if Thread Apartment model is MTA:
http://connect.microsoft.com/VisualStudio/feedback/details/96991/net-2-0-beta-2-mtathread-and-openfiledialog
Every functionality that can be made by user over GUI, can also be done through Automation. In order to attach a file to Word Document, you would use "Insert Object" (http://www.ehow.com/how_5877175_add-attachments-ms-word.html).
Here is the Word Interop Reference. Maybe you can find your answer there.
I'm using dokan as a file system and don't know how to identify office file write operation. Specially the lock and temp files.
I have to identify them because only the main file has to upload to an external database.
Theoretically I get office word to work but this is more a workaround. I hope there is a special attribute to identify there temp files.
Regard Chris
You can check the ProccessId to see if its Office's proccess. Check if the filename matches temp file. Some Office versions call CreateFile with FILE_FLAG_TEMPORARY and som set it later in SetAttributes. The best way is delay file upload till Cleanup and even then you can add an timer to uplosd it couple of seconds later. (Not sure but I think I remember that OpenOffice closes temp file and then opens it again to call MoveFile).
Hope it helps.
I'm using Interop to create a "custom word editor", basically I've incorporated MS Word on my application and I allow users to edit a document and insert custom fields from a predefined list.
I provide a "Preview" option to see how the document will appear when the data is added.
The users can start editing a template on my application, and at any time they can hit preview and the preview should appear with the latest changes.
I want the user to be able to preview the document without saving the changes, the problem is that when I invoke SaveAs() on the document (to create a temp file that I can use as the input for the preview generator), the editor opens the temp document.
Is there a way to save a copy of the document being edited but keep the original (open) document with its changes unsaved?
Thanks a lot
I know this can be done in pre-2007 Word, as described in this post.
Unfortunately, that solution does not work in Word 2007.
How about using a temp file from the get-go, and only saving to the "true" file when the user indicates that he or she is done?