'插入表格
Sub setTable()
Set myRange = ActiveDocument.Range(Start:=2, End:=2)
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4
End Sub
'取得Word常規字符串
Sub getText()
Set myRange = ActiveDocument.Range(Start:=0, End:=4)
MsgBox myRange.Text
End Sub
'取得Word 表格中的數據
Sub getTableCellText()
Dim s
For i = 1 To ActiveDocument.Tables.Count
For iRow = 1 To ActiveDocument.Tables(i).Rows.Count
For icol = 1 To ActiveDocument.Tables(i).Columns.Count
Set myCell = ActiveDocument.Tables(i).Cell(Row:=iRow, Column:=icol)
s = s & Mid(myCell.Range.Text, 1, Len(myCell.Range.Text) - 2)
Next icol
Next iRow
Next i
MsgBox s
End Sub
Cell的屬性RowIndex和ColIndex來取得某格單元格在表中的行列號
========================== Delphi ===================================
WordApp: TWordApplication;
WordDoc: TWordDocument;
DocInx,oFileName,CfCversions,oReadOnly,AddToRctFiles,PswDocument,
PswTemplate,oRevert,WPswDocument,WPSwTemplate,oFormat: OleVariant;
myRange:Range;
myCell:Cell;
myRow:Row; myCol:Column;
if not Assigned(WordApp) then // ===== 創建對象 =====
begin
WordApp:= TWordApplication.Create(nil);
WordApp.Visible := false;
end;
if not Assigned(WordDoc) then
begin
WordDoc:= TWordDocument.Create(nil);
end;
DocInx:=1;
oFileName := InFile;
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)); // ===== 關聯文件 =====
//取整個文本的字符內容,包含表格
s := WordDoc.Range.text;
//取 1 -- 4 位的字符 ,包含表格
myRange:=WordDoc.Range;
myRange.Start:=0;
myRange.End_ :=4;
//方法(1)==> 規則表
For i := 1 To WordDoc.Tables.Count do
begin
For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do //第 i 個表 iRow行
begin
For icol := 1 To WordDoc.Tables.Item(i).Columns.Count do //第 iCol列
begin
myCell:=WordDoc.Tables.Item(i).Cell(iRow,icol);
memo1.Lines.add(myCell.Range.Text);
end;
end;
end;
//方法(2)==> 不規則表:只有橫向合並時
For i := 1 To WordDoc.Tables.Count do //第 i 個表
begin
For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do
begin
myRow:=WordDoc.Tables.Item(i).Rows.Item(iRow); //第 iRow 行
For icol := 1 To myRow.Cells.Count do //第 iCol列
begin
myCell:= myRow.Cells.Item(iCol) ;
memo1.Lines.add(myCell.Range.Text);
end;
end;
end;
//方法(3)==> 不規則:橫向、縱向合並時
For i := 1 To WordDoc.Tables.Count do
begin
for j := 1 To WordDoc.Tables.Item(i).Range.Cells.Count do
begin
myCell := WordDoc.Tables.Item(i).Range.Cells.Item(j);
memo1.Lines.add(myCell.Range.Text);
end;
end;
//合並第一、二列
iStart:=WordDoc.Tables.Item(i).Cell(1,1).Range.Start;
myCol:= WordDoc.Tables.Item(i).Columns.Item(2);
IEnd:=myCol.Cells.Item(myCol.Cells.Count).Range.End_;
myRange:=WordDoc.Range;
myRange.Start:=iStart;
myRange.End_ :=IEnd;
myRange.Cells.Merge;
if Assigned(WordDoc) then // ===== 關閉文件 =====
begin
WordDoc.Close;
WordDoc.Disconnect;
WordDoc.Destroy;
WordDoc := nil;
end;
if Assigned(WordApp) then // ===== 關閉Word =====
begin
WordApp.Quit;
WordApp.Disconnect;
WordApp.Destroy;
WordApp := nil;
end;
/////////////////////////////
For i := 1 To WordDoc.Tables.Count do //第 i 個表
begin
For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do
for iCol:=1 to WordDoc.Tables.Item(i).Columns.Count do
myCell:=WordDoc.Tables.Item(i).Cell(iRow,iCol); //取[iRow,iCol]列值
end;