I am trying to read an Excel sheet embedded in a Word document. What do I do after opening the Word document? These are the references I am using (Excel,Word)-- are there any others I need to use?
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range range;
Word.Application wordApp;
Word.Document wordDoc;
Word.Range wordRange;
public TestCase Test()
{
wordApp = new Word.Application();
wordDoc = wordApp.Documents.Open(@"document");
what next??
}
Embedded documents are represented by inline shapes in the document, so you can access them in the following way:
Sub Extract()
Dim num as Integer
Dim AD as document
Set AD = activedocument
Dim numObjects As Integer
numObjects = AD.InlineShapes.count
MsgBox numObjects ' prints "11"
For num = 1 To numObjects
If AD.InlineShapes(num).Type = 1 Then
'it's an embedded OLE type so open it.
AD.InlineShapes(num).OLEFormat.Open
End If
Next num
End Sub
In order to interact with the embedded worksheet, it's necessary to Activate
it.
Once it's activated , you can handle the Excel.Workbook
, Excel.Application
and Excel.Worksheet
and Excel.Range
objects:
static void Main()
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
Excel.Range xlRange;
Word.Application wordApp = new Word.Application();
wordApp.Visible = true;
Word.Document wordDoc = wordApp.Documents.Open(@"c:\debug\word-excel.docm");
// activate the object before you can interact with it
wordDoc.InlineShapes[1].OLEFormat.Activate();
xlWorkBook = wordDoc.InlineShapes[1].OLEFormat.Object;
xlApp = xlWorkBook.Parent;
xlWorkSheet = xlWorkBook.Worksheets[1];
xlRange = xlWorkSheet.Range["A1:D10"];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.