Temporarily save Word document changes - c#

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?

Related

How to insert custom control into Office Word document

I want to insert my custom control into the currently opened Word document using VSTO. I have an application which communicates with my already existing VSTO Word plugin over COM. So far i have been using a workaround where i inserted pictures of my custom control as a InlineShape (as images) into the document. This was fine for now, since Word was automatically saving it inside the document, took care of resizing, deleting with the delete key, drag droping image as a singe character and so on... But now i am encountering some problems regarding adding new functionalities (eg. Right click on control (image) click). Now i am looking for an alternatives, how to insert custom control (probably an ActiveX control?) .
I have created an example when i insert a custom control with the Microsoft.Office.Tools.Word.Document.Controls.AddControl() function, but here are some problems:
The control can not be dragged around the range of the document
The control is not saved when the document is closed and reopened
Control does not work when the document zoom is not 100%
See the picture below:
I have also noticed, that i can insert an object into the current word document (insert -> object -> eg. Microsoft Word Document, see picture below)
This inserted object has all the functionality which i need (drag-drop as a single character, saving on document close, resize, functionality when document zoom is not 100%,...) This is exactly what i want to achieve, but instead of another Microsoft Word object, I would like to display my custom control. See the picture below for comparison between MS Word Document object and my custom control inside opened document:
Now i have already spent quite some time googling and doing the research but i am running out of ideas on how to successfully create this to work... If anyone is familiar with the solution of could provide any details on it, it would be extremelly helpful! I have also tried doing something with the new plugins Office add-ins, but keep in mind that i am developing a Windows app, i do not need crossplatform functionality and Word plugin needs to be accessible from the other app (just like current solution, where i access it using COM). Hopefully the explanation was clear enough, i am more than ready to provide additional information if needed!

How to open Word document from memory stream

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.

How to insert image in a existing word document with c#

I am working with word and c# . taking the snap shot with my code and saving it in a particular folder (ex.C:\Temp). now i want to save the image to an existing word document.any kind of help with short code sample is highly appreciated .
Will prefer to use Microsoft.Office.Interop.Word;
Of course, the Word object model provides the required methods for inserting an image into the document. To add a picture at the cursor location you just need to call the AddPicture method of the InlineShapes collection and pass in the name of the file.
Application.Selection.InlineShapes.AddPicture(#"C:\SamplePicture.jpg");
See How to: Programmatically Add Pictures and Word Art to Documents for more information.
Also you may consider using the Open XML SDK. Take a look at the following articles in MSDN:
How to: Insert a picture into a word processing document (Open XML SDK)
Adding Images to Documents in Word 2007 by Using the Open XML SDK 2.0 for Microsoft Office

Write data into current Microsoft Word document using Project Add-in

I am doing POC on Project Add-in for Microsoft Word. I need to write some data into Microsoft Word document, wherever user places cursor on document. But, I am unable to read the opened the document and cursor position. How to do that?
I am able to write into new document, by initializing the new document, but I need to write data into any document, no matter whether it is a new document or opened one.

Auto Fill Webform through Word doc in Asp.net

hey i'm working on project in which I have webform which includes some editfields. I don't want to enter the data manually into that editfield. what I want is to extract data from a word document and fill that editfields. But the catch is, through which MS doc I fill the editfield?
Suppose We have a bunch of lectures uploaded on some page. so what should I do to retrieve the data from a particular document?
Is it necessary to open the MS-doc file first?
or I should download the file first?
If I goes with option 1 when should I have to use some library? what opens the file within browser, retrieve the data and the a pop-up message appears "the data has been retrieved now you can close the file". and next I can fill the form with that data.
or should I goes with the 2 Option when an individual hit the download button then the file will be stored into the local machine. how can I keep the track that which ms-file is downloaded or stored into the local machine?and is it necessary to open that file for retrieving the data again?
These are my point of views that how I can implement that module. So I need your suggestions? Is this the right way to achieve this goal or should I follow the other path? and which libraries are required to achieve this task or any tutorial similar to this problem ?
Thanks in Advance
I would suggest considering a third option: since the Word document files exist on the server, the cleanest place to pre-populate a form would be by extracting data from the document while it is on the server and filling in the form's fields before sending it down to the user in a codebehind. Trying to extract data on the client side from a recently-downloaded file via an application other than the browser seems ripe for kludgy-ness. Articles such as http://support.microsoft.com/kb/257757 should help get you started in the right direction.
For extracting data from ms word document using free .net word component and fill data to webform,
extract data,
Document doc = new Document();
doc.LoadFromFile("YouDocOrDocx.Docx");
string content = doc.GetText();

Categories

Resources