问题
I am currently trying to edit cells of an excel spreadsheet object with c# interop. I inserted it in a word document as an object.
Until there i didn't succeed to programm anything that really works. I'm able to select the component but i can't open it to edition and then reach grid's cells.
I use a button control in a custom office ribbon to launch edit. Here is my method:
public void EditTable(Office.IRibbonControl control)
{
Word.Application oWordApp = (Word.Application)Marshal.GetActiveObject("Word.Application");
Word.Document oWordDoc = oWordApp.ActiveDocument;
Word.Bookmark ReqsBookmark = DocumentHelper.GetBookmark("test");
ReqsBookmark.Select();
}
The only way i know to access a specific object with interop is with bookmarks.
Does anybody have an idea of how doing such a thing?
回答1:
In Word, an Excel worksheet (workbook) is "wrapped" in an OLE control that is a member of the InlineShapes
or Shapes
collection. So you need the AddOLEObject
method of the collection you want to use.
Access to the object model of the OLE server (Excel) is through the OLEFormat
property of the InlineShape
or Shape
. So your code would be something like the sample below.
Note that although you say this is a VSTO project, the code you show us is not VSTO. You're starting up a new instance of the Word.Application, but the VSTO Add-in would be running in-process. My code is VSTO code, but can certainly be adjusted for other situations...
{
Word.Document doc = Globals.ThisAddIn.app.ActiveDocument;
object oRngTarget = Globals.ThisAddIn.app.Selection.Range;
//object oRngTarget = DocumentHelper.GetBookmark("test").Range;
object oOLEClass = "Excel.Sheet.12";
object oFalse = false;
Word.InlineShape ils = doc.InlineShapes.AddOLEObject(ref oOLEClass, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref oRngTarget);
Word.OLEFormat olef = ils.OLEFormat;
System.Globalization.CultureInfo oldCI= System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Excel.Workbook wb = (Excel.Workbook)olef.Object;
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];
try
{
ws.get_Range("A1").Value2 = "New category";
ws.get_Range("B1").Value2 = 6.8;
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print(ex.Message);
}
finally
{
ws = null;
wb = null;
ils = null;
doc = null;
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;
}
}
To later work with a spreadsheet in the Word document, you basically follow the same principle: declare and instantiate a InlineShape.OLEFormat
object, Activate it, then cast olef.Object
to an Excel.Workbook:
olef.Activate();
Excel.Workbook wb = (Excel.Workbook)olef.Object;
回答2:
I finally succeeded thanks to this post about modify an embedded Excel object inside a Word doc
Here's the c# method if anybody need it one day:
{
Word.Document oWordDoc = Globals.ThisAddIn.Application.ActiveDocument;
Excel.Workbook oOLE = null;
Excel.Worksheet oSheet = null;
Word.InlineShapes ils = oWordDoc.InlineShapes;
ils[1].OLEFormat.Activate();
oOLE = ils[1].OLEFormat.Object;
oSheet = oOLE.Worksheets[1];
oSheet.get_Range("A1").Value = "I did it too!";
}
Thank you again @CindyMeister for your answer, it helped me to understand how it really works.
来源:https://stackoverflow.com/questions/37272419/edit-excel-spreadsheet-object-in-word-document-c-interop