4
votes

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?

3

3 Answers

2
votes

Below code works for me:

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

string XML = doc.Content.WordOpenXML;

// XML Changes

doc.Content.InsertXML(XML);
1
votes
        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.

0
votes

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