Home:ALL Converter>VSTO add-in, access to both Excel and Word

VSTO add-in, access to both Excel and Word

Ask Time:2018-04-06T06:32:32         Author:PTLEng

Json Formatter

I'm working on a VSTO add-in for Word 2010 (.NET Framework 4.6.1) in C#. This add-in is meant to produce Word documents from a template, using data from a few sources, a few of which (depending on the ) being one or more Excel workbooks.

I have been perusing the MSDN literature, but I feel as if I'm missing a crucial step.

Is it possible to access the Microsoft.Office.Tools.Word objects in the same add-in as Microsoft.Office.Tools.Excel objects? They each seem to rely on the Factory.GetVstoObject() method, which uses a factory object that is application-specific. I know I can use the normal Microsoft.Office.Interop.Word/Excel objects, but they aren't as useful.

I have been playing around with having two add-ins, one targeting Word and one targeting Excel, but I'm having a hard time getting them to see each other. If this isn't achievable as a VSTO add-in, can you reference multiple Office application object models in a standalone executable?

Author:PTLEng,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/49682509/vsto-add-in-access-to-both-excel-and-word
user585968 :

\n Is it possible to access the Microsoft.Office.Tools.Word objects in the same add-in as Microsoft.Office.Tools.Excel objects\n\n\nMaybe not those exact types but you can find something similar. You can always drop down to pure COM/Ole Automation level completely by-passing VSTO APIs. Office is still exposing a COM API which VSTO I suspect is just wrapping into nicer c# classes.\n\ne.g. from a c# Excel VSTO add-in you can fire up word document by:\n\ntry\n{\n var type = Type.GetTypeFromProgID(\"Word.Application\");\n dynamic app = Activator.CreateInstance(type);\n app.Visible = true;\n\n dynamic doc = app.Documents.Add();\n doc.Content.InsertAfter(\"Hello world!\");\n}\ncatch (Exception ex)\n{\n MessageBox.Show(ex.ToString());\n}\n\n\nNote: I am using dynamic (one of the main purposes of dynamic in c# was to make COM easier) here so as to avoid dependence on any specific COM-type library. It's version neutral so it should work any Word version.\n\n\n I know I can use the normal Microsoft.Office.Interop.Word/Excel objects, but they aren't as useful.\n\n\nIn what sense?\n\nSee also\n\n\nWhere to find OLE Automation's documentation, specifically this answer\n",
2018-04-06T00:10:14
yy