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

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.

Related

What is the Pre-Open-Event for files in Word VSTO

I'm trying to make an add-in in C# for Word and I need to process things before that document has been opened by Word.
There is an Open event, but it is called/executed after the document has been opened by Word.
But I need to process data before the document is opened.
Does anybody now how this kind of event is named?

VSTO Add-in + Open XML

I would like to develop some add-in. This add-in should use open xml (not everything is doable using VSTO + Open XML is much faster), BUT is there any possibility to edit opened document? For example I have opened PowerPoint presentation (or Word doc/Excel spreadsheet) and I would like to replace some text using Open XML, can I read it (from that what I read, reading is possible) and update without closing that file, using Open XML of course? If yes, how to do it?
There is some feeddata() method, memory streams, but I do not know how to use it. Could anyone show some example?
Below code works for me:
Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
string XML = doc.Content.WordOpenXML;
// XML Changes
doc.Content.InsertXML(XML);
var fileFullName = Globals.ThisAddIn.Application.ActiveDocument.FullName;
Globals.ThisAddIn.Application.ActiveDocument.Close(WdSaveOptions.wdSaveChanges, WdOriginalFormat.wdOriginalDocumentFormat, true);
//edit document using OpenXml here
Globals.ThisAddIn.Application.Documents.Open(fileFullName);
This works for Word documents.
The purpose of VSTO and Open XML are different.
VSTO is basically extending office to your purpose where you run addin in the context of the Office application. And you can make use of Com interops as they are already available.
Open XML is used for automation like generating or editing docs, workbooks where you dont have office application installed. for eg in Asp.net server you dont need to install Office but use Open XML for creating, editing docs, spreadsheets or ppt.
In your case if you going for VSTO approach you dont need to use Open XML sdk. Use the available find and replace functionality in the corresponding interops.
After replace dont forget to save the doc, xls or ppt
Word
Excel
Powerpoint

Copy content from some other word file into currently active word document without opening the other word file

I am creating an Add In for Microsoft Word and I have an active document open in my Word Application object.
using tw = Microsoft.Office.Tools.Word;
using w = Microsoft.Office.Interop.Word;
using wp = DocumentFormat.OpenXml.Wordprocessing;
tw.Document doc =
Globals.Factory.GetVstoObject(Globals.ThisAddIn.Application.ActiveDocument);
I need to copy some content from another word document file into this doc object at current cursor position, without opening that source document file.
Update:-
By saying, without opening the source file, I am making a work around for me, and yes, that means, I don't want to show that source file open in MS Word - with a Word Method that will do this automatically if it exist, without making a hack of opening the Source Word file invisibly.
Originally, what I need is - I have that source file saved as a Binary Object (BLOB) in my database, and would like to load it directly in a MemoryStream and copy contents from it to the Active Word Document. But, because I am not getting a way to do this, I thought I will save that source file temporarily on the disk, and then copy its contents into the active document, provided it is not visible in the MS Word while copying.
Is this possible?
Any API available for doing this?

How to programmatically interact with an open MS Word document

I am trying to highlight a word(e.g. a custom search function) in an open word document.
I'd like to know if I can make it happen by single clicking the word.
You can format text using a document-level customization. Read this article

Temporarily save Word document changes

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?

Categories

Resources