Author: Tomas Rutkauskas
I have an Excel object in a Word document. I want to read the content of the Excel
object from my Delphi program. How can I do that?
Answer:
1 { ... }
2 var
3 AWordApplication: WordApplication;
4 AWordDocument: WordDocument;
5 AWorkBook: ExcelWorkBook;
6 AWorkSheet: ExcelWorkSheet;
7 AInlineShape: InlineShape;
8 AFileName: OleVariant;
9 TrueParam: OleVariant;
10 begin
11 AWordApplication := CoWordApplication.Create;
12 try
13 FalseParam := False;
14 AFileName := 'c:\wordexcel.doc';
15 AWordDocument := AWordApplication.Documents.Open(AFileName, EmptyParam,
16 EmptyParam,
17 EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
18 EmptyParam, EmptyParam, EmptyParam, EmptyParam);
19 AInlineShape := AWordDocument.InlineShapes.Item(1);
20 AInlineShape.Activate;
21 AWorkBook := AWordDocument.InlineShapes.Item(1).OLEFormat.Object_ as
22 ExcelWorkBook;
23 AWorkSheet := AWorkBook.ActiveSheet as ExcelWorkSheet;
24 ShowMessage(AWorkSheet.Cells.Item[2, 1].Text);
25 finally
26 AWordApplication.Quit(FalseParam, EmptyParam, EmptyParam);
27 AWordApplication := nil;
28 AWordDocument := nil;
29 end;
30 end;
|