1.连接到Word应用程序
DocInx: OleVariant; word_path: OleVariant;
//打开word文件
wordApp1.Connect; word_path := fileName; DocInx := 1; //打开指定的word文件 wordApp1.Documents.Open(word_path, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam); wordApp1.Visible := False;//在操作时使文件不可见 //关联word文件 DocInx := 1; wordDoc1.ConnectTo(wordApp1.Documents.Item(DocInx));
2.操作Word
tableCount:=wordDoc1.Tables.Count;//获得word中表格的个数 if Assigned(wordDoc1.Tables.Item(i)) then//判断表格是否有效 begin tmpStr := Trim(delchar39(wordDoc1.Tables.Item(i).Cell(2, 2).Range.Text));//获得第i个表格中第二行第二列的值 end;
3.断开和word的连接
wordDoc1.Disconnect; wordApp1.Quit; wordApp1.Disconnect;