Delphi與Word(三)取得Word檔案的資料 (轉)

amyz發表於2007-08-15
Delphi與Word(三)取得Word檔案的資料 (轉)[@more@]

//取得的資料
procedure getWordStr;
var WordApp: TWordApplication;
  WordDoc: TWordDocument;
  DocInx,oFileName,CfCversions,oReadOnly,AddToRctFiles,PswDocument,
  PswTemplate,oRevert,wDocument,WPswTemplate,oFormat: OleVariant;
  i,iRow,iCol:integer;
  myCell:Cell;
  myRow:Row;
begin
  memo1.Lines.Clear ;

  // ===== 建立 =====
  if not Assigned(WordApp) then 
  begin
  WordApp:= TWordApplication.Create(nil);
  WordApp.Visible := false;
  end;
  if not Assigned(WordDoc) then
  WordDoc:= TWordDocument.Create(nil);
  try
  DocInx:=1;
  oFileName := 'd: est.doc';
  oReadOnly:=true;
  CfCversions := EmptyParam;
  AddToRctFiles:= EmptyParam;
  PswDocument:= EmptyParam;
  PswTemplate:= EmptyParam;
  oRevert:= EmptyParam;
  WPswDocument:= EmptyParam;
  WPswTemplate:= EmptyParam;
  oFormat:= EmptyParam; 
  // ===== 開啟檔案 ===== 
  WordApp.Documents.open(oFileName,CfCversions,oReadOnly,AddToRctFiles,
  PswDocument,PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat);
  // ===== 關聯檔案 =====
  WordDoc.ConnectTo(WordApp.Documents.Item(DocInx));


  //方法(1): 取整個文字的字元內容,包含表格
  s := WordDoc.Range.text; 

  //方法(2): 取 1 -- 4 位的字元 ,包含表格
  myRange:=WordDoc.Range;
  myRange.Start:=0;
  myRange.End_ :=4;

  finally
  if Assigned(WordDoc) then  // ===== 關閉檔案 =====
  begin
  WordDoc.Close;
  WordDoc.Dinnect;
  WordDoc.Destroy;
  WordDoc := nil;
  end;
  if Assigned(WordApp) then  // ===== 關閉Word =====
  begin
  WordApp.Quit;
  WordApp.Disconnect;
  WordApp.Destroy;
  WordApp := nil;
  end;
  end;
end;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-958541/,如需轉載,請註明出處,否則將追究法律責任。

相關文章