ViaThinkSoft CodeLib
This article is in:
CodeLib → Programming aids → Delphi
uses
Word_TLB;
function GetCellText(aCell: Cell; toClipboard: boolean=false): string;
var
aRange: WordRange;
count: OleVariant;
aWdUnits: OleVariant;
begin
count := -1;
aWdUnits := wdCharacter;
aRange := aCell.Range;
aRange.MoveEnd(aWdUnits, count); // This is needed so border is not included
result := aRange.Text;
if toClipboard then
begin
aRange.Application.Selection.Range.SetRange(aRange.Start, aRange.End_);
aRange.Copy;
end;
end;
procedure ShowFirstCells(FileName: string);
var
wordApp: _Application;
doc: WordDocument;
table: Word_TLB.Table;
filename: OleVariant;
readOnly: OleVariant;
iTable: integer;
begin
filename := '"'+FileName+'"';
try
wordApp := CoWordApplication.Create;
wordApp.visible := False;
readOnly := true;
doc := wordApp.documents.open( filename, emptyparam, readonly,emptyparam,
emptyparam,emptyparam,emptyparam,emptyparam,
emptyparam,emptyparam,emptyparam,emptyparam,
emptyparam,emptyparam,emptyparam,emptyparam );
if doc = nil then
begin
raise Exception.Create('Cannot open Word document!!');
end;
for iTable := 1 to doc.Tables.Count do
begin
table := doc.tables.item(iTable);
ShowMessageFmt('First cell of table #%d is: %s', [iTable, GetCellText(table.cell(1,1))]);
end;
finally
wordApp.quit(EmptyParam, EmptyParam, EmptyParam);
end;
end;
Tested with Microsoft Word 2010.
To create the Word_TLB.pas file, please run following command:
tlibimp -p+ -R+ -O+ -Cw+ -Hr+ -Hs+ "C:\Program Files (x86)\Microsoft Office\Office14\msword.olb"
The directory where the msword.olb file is located, differs from installations and versions of Microsoft Word.
Daniel Marschall
ViaThinkSoft Co-Founder
ViaThinkSoft Co-Founder