在WORD插入图片你看Delphi的例子,合并表格你看我的例子.在WORD中合并表格是:
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);//先选定范围
WA.Selection.Cells.Merge;//合并
你可以先看WORD的宏代码,可以对你有所帮助.
以前写的一个在WORD中画表和填表的例子,写得比较乱,你只需要看部分内容知道怎么做就行了.
Delphi的例子中也有一个讲调用WORD的.
procedure TFrmCompany.WriteToWord;
var Template,NewTemplate,ItemIndex,PageNumberAlignment:OleVariant;
Count_Var:OleVariant;
un_Var,un_DownVar,ex_Var,Start_var,End_Var,Down_Var,Up_Var:OleVariant;
Bool_var:OleVariant;
i:integer;
CWidth:array[0..8] of single;
begin
CWidth[0]:=45;
CWidth[1]:=80;
CWidth[2]:=35;
CWidth[3]:=240;
CWidth[4]:=400;
CWidth[5]:=355;
CWidth[6]:=150;
CWidth[7]:=275;
CWidth:=55;
Template := EmptyParam;
NewTemplate := False;
ItemIndex:=1;
WA.Connect;
WA.Visible:=True;
WA.Documents.Add(Template, NewTemplate);
WD.ConnectTo(WA.Documents.Item(ItemIndex));
PageNumberAlignment:=wdAlignPageNumberCenter;
Bool_var:=True;
WA.Selection.Sections.Item(1).Footers.Item(1).PageNumbers.Add(PageNumberAlignment,Bool_Var);
Bool_var:=False;
AdoQryCompany.First;
i:=1;
while not AdoQryCompany.Eof do
begin
WD.Range.InsertAfter(#13);
WD.Tables.Add(WA.selection.Range,9,4);
un_Var:=wdCharacter; //第一行
Start_Var:=1;
End_Var:=4;
ex_Var:=wdExtend;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.TypeText(AdoQryCompany.FieldByName('province').AsString);
WA.Selection.Cells.Item(1).SetWidth(CWidth[4],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
un_DownVar:=wdLine;
Down_Var:=1;
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var); //第二行
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[2],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[7],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
Start_Var:=1;
End_Var:=3;
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
ex_Var:=wdExtend;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.Cells.Item(1).SetWidth(CWidth[5],wdAdjustNone);
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone); //第四行
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
Count_Var:=3;
WA.Selection.Cells.Split(Down_var,Count_var,Bool_Var);
WA.Selection.MoveLeft(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[1],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[2],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[6],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone); //第五行
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[1],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[2],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[3],wdAdjustNone);
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var); //第六行
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[2],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[7],wdAdjustNone);
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone);
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
End_Var:=3;
ex_Var:=wdExtend;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.Cells.Item(1).SetWidth(CWidth[5],wdAdjustNone);
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var); //第八行
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone); //第七行
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
End_Var:=3;
ex_Var:=wdExtend;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.Cells.Item(1).SetWidth(CWidth[5],wdAdjustNone);
ex_Var:=wdMove;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
WA.Selection.Cells.Item(1).SetWidth(CWidth[0],wdAdjustNone); //第七行
WA.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
WA.Selection.MoveRight(un_Var,Start_Var,ex_Var);
End_Var:=3;
ex_Var:=wdExtend;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.Cells.Item(1).SetWidth(CWidth[5],wdAdjustNone);
ex_Var:=wdMove;
End_Var:=3;
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
if txtOK then
WD.Tables.Item(i).Cell(2,1).Range.Text:=lblData[0]
else
WD.Tables.Item(i).Cell(2,1).Range.Text:='ID';
WD.Tables.Item(i).Cell(2,2).Range.Text:=AdoQryCompany.FieldByName('ID').AsString;
if txtOK then
WD.Tables.Item(i).Cell(2,3).Range.Text:=lblData[1]
else
WD.Tables.Item(i).Cell(2,3).Range.Text:='Name';
WD.Tables.Item(i).Cell(2,4).Range.Text:=AdoQryCompany.FieldByName('name').AsString;
if txtOK then
WD.Tables.Item(i).Cell(3,1).Range.Text:=lblData[5]
else
WD.Tables.Item(i).Cell(3,1).Range.Text:='Address';
WD.Tables.Item(i).Cell(3,2).Range.Text:=AdoQryCompany.FieldByName('address').AsString;
if txtOK then
WD.Tables.Item(i).Cell(4,1).Range.Text:=lblData[6]
else
WD.Tables.Item(i).Cell(4,1).Range.Text:='Tel';
WD.Tables.Item(i).Cell(4,2).Range.Text:=AdoQryCompany.FieldByName('tel').AsString;
if txtOK then
WD.Tables.Item(i).Cell(4,3).Range.Text:=lblData[7]
else
WD.Tables.Item(i).Cell(4,3).Range.Text:='Fax';
WD.Tables.Item(i).Cell(4,4).Range.Text:=AdoQryCompany.FieldByName('fax').AsString;
if txtOK then
WD.Tables.Item(i).Cell(4,5).Range.Text:=lblData[17]
else
WD.Tables.Item(i).Cell(4,5).Range.Text:='Post No';
WD.Tables.Item(i).Cell(4,6).Range.Text:=AdoQryCompany.FieldByName('postno').AsString;
if txtOK then
WD.Tables.Item(i).Cell(5,1).Range.Text:=lblData
else
WD.Tables.Item(i).Cell(5,1).Range.Text:='LXR';
WD.Tables.Item(i).Cell(5,2).Range.Text:=AdoQryCompany.FieldByName('lxr').AsString;
if txtOK then
WD.Tables.Item(i).Cell(5,3).Range.Text:=lblData[9]
else
WD.Tables.Item(i).Cell(5,3).Range.Text:='Tel';
WD.Tables.Item(i).Cell(5,4).Range.Text:=AdoQryCompany.FieldByName('l_tel').AsString;
if txtOK then
WD.Tables.Item(i).Cell(6,1).Range.Text:=lblData[11]
else
WD.Tables.Item(i).Cell(6,1).Range.Text:='FeedBack';
WD.Tables.Item(i).Cell(6,2).Range.Text:=AdoQryCompany.FieldByName('feedback').AsString;
WD.Tables.Item(i).Cell(6,3).Range.Text:='Email';
WD.Tables.Item(i).Cell(6,4).Range.Text:=AdoQryCompany.FieldByName('email').AsString;
if txtOK then
WD.Tables.Item(i).Cell(7,1).Range.Text:=lblData[16]
else
WD.Tables.Item(i).Cell(7,1).Range.Text:='Memo';
WD.Tables.Item(i).Cell(7,2).Range.Text:=AdoQryCompany.FieldByName('memo').AsString;
if txtOK then
WD.Tables.Item(i).Cell(8,1).Range.Text:=lblData[18]
else
WD.Tables.Item(i).Cell(8,1).Range.Text:='Products';
WD.Tables.Item(i).Cell(8,2).Range.Text:=AdoQryCompany.FieldByName('products').AsString;
if txtOK then
WD.Tables.Item(i).Cell(9,1).Range.Text:=lblData[10]
else
WD.Tables.Item(i).Cell(9,1).Range.Text:='Notice';
WD.Tables.Item(i).Cell(9,2).Range.Text:=AdoQryCompany.FieldByName('notice').AsString;
Down_Var:=1;
ex_Var:=wdMove;
ex_Var:=wdMove;
AdoQryCompany.Next;
inc(i);
end;
end;