您的位置:首页 > 编程语言 > Delphi

使用Delphi获取Word文件中的数据

2008-06-11 00:00 405 查看
//delphi7、Office2003下编译通过

//参考http://dev.csdn.net/article/18/18811.shtm,并做了修改

//取得Word文件的数据 需要uses OleCtnrs;
procedure TForm1.getWordStr;
var
WordA : TWordApplication;
WordDoc: TWordDocument;
DocInx,oFileName,CfCversio ,oReadOnly,AddToRctFiles, wDocument,
wTemplate,oRevert,WwDocument,WwTemplate,oFormat,olevar: OleVariant;
i,iRow,iCol:integer;
myCell:Cell;
myRow:Row;
s:string;
myRange: Range;
begin
// ===== 创建对象 =====
if not Assigned(WordA ) then
begin
WordA := TWordApplication.Create(nil);
WordA .Visible := false;
end;
if not Assigned(WordDoc) then
WordDoc:= TWordDocument.Create(nil);
try
DocInx:=1;
oFileName := 'c:/1.doc';
oReadOnly:=true;
CfCversio := EmptyParam;
AddToRctFiles:= EmptyParam;
wDocument:= EmptyParam;
wTemplate:= EmptyParam;
oRevert:= EmptyParam;
WwDocument:= EmptyParam;
WwTemplate:= EmptyParam;
oFormat:= EmptyParam ;
// ===== 打开文件 =====
WordA.Documents.Open(oFileName,CfCversio ,oReadOnly,AddToRctFiles,wDocument, wTemplate,oRevert,WwDocument,WwTemplate,oFormat,olevar,olevar,olevar,olevar,olevar);
// ===== 关联文件 =====
// WordDoc.ConnectTo(WordA.Documents.Item(DocInx));

WordA.Selection.WholeStory;
WordA.Selection.Copy;
Richedit1.PasteFromClipboard;
s:=Richedit1.Text ;
//s:= WordA.Selection.Text;
Showmessage(s);
finally
if Assigned(WordDoc) then // ===== 关闭文件 =====
begin
WordDoc.Close;
WordDoc.Disconnect;
WordDoc.Destroy;
WordDoc := nil;
end;
if Assigned(WordA ) then // ===== 关闭Word =====
begin
WordA .Quit;
WordA .Disconnect;
WordA .Destroy;
WordA := nil;
end;
end;
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: