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

Delphi 与 Word_VBA

2012-04-16 14:25 218 查看
'插入表格

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;

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: