你可以用这个来控制长度.WD.Tables.Item(i).Cell(4,4).range.Columns.Width:=55;
这是我以前用过的,因为表比较复杂,不但有合并且每行宽度并不一样,所以下面比较杂乱,
但看看代码一定有收获.若你的每列都对齐的话,那很简单.最好先算出列宽再定义就最好.
若表格比较复杂,那就比较麻烦.若这样的话,我是不太提倡用WORD来生成表格,不如用EXCEL
或其他专用打印工具.
procedure TFrmCompany.WriteToWord;
var Template,NewTemplate,ItemIndex,PageNumberAlignment:OleVariant;
// MRange:Variant;
Count_Var:OleVariant;
// Table:OleVariant;
// un_Var,ex_Var,cnt_Var,
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[8]:=55;
Template := EmptyParam;
// NewTemplate := True;
NewTemplate := False;
ItemIndex:=1;
// try
WA.Connect;
{ except
WA.Disconnect;
ShowMessage('Please try again!');
abort;
end;}
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);
// Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=True
Bool_var:=False;
AdoQryCompany.First;
i:=1;
// for i:=1 to AdoQryCompany.RecordCount do
while not AdoQryCompany.Eof do
begin
// WD.Tables.Add(WD.Range,7,4);
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.MoveDown(un_DownVar,Down_Var,ex_Var); //第二行不變
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.MoveDown(un_DownVar,Down_Var,ex_Var); //第三行
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.MoveLeft(un_Var,Start_Var,ex_Var);
WA.Selection.MoveRight(un_Var,End_Var,ex_Var);
WA.Selection.Cells.Merge;
WA.Selection.Cells.Item(1).SetWidth(CWidth[5],wdAdjustNone);
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var);
// WA.Selection.MoveLeft(un_Var,Start_Var,ex_Var);
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);
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var); //第七行
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);
// Down_Var:=1; //第九行
// ex_Var:=wdMove;
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var);
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);
// Start_Var:=1;
// ex_Var:=wdMove;
// 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[8]
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;
{ WD.Tables.Item(i).Cell(4,1).range.Columns.Width:=35;
WD.Tables.Item(i).Cell(4,2).range.Columns.Width:=55;
WD.Tables.Item(i).Cell(4,3).range.Columns.Width:=35;
WD.Tables.Item(i).Cell(4,4).range.Columns.Width:=55;
WD.Tables.Item(i).Cell(4,5).range.Columns.Width:=35;
WD.Tables.Item(i).Cell(4,6).range.Columns.Width:=45;}
Down_Var:=1;
ex_Var:=wdMove;
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var);
// WD.Range.InsertAfter(#13);
{ if ex_Var<>0 then showmessage('!!!');
try
WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var); //提一行
except
ShowMessage(AdoQryCompany.FieldByName('tel').AsString);
// ex_Var:=wdMove;
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var); //提一行
end;}
// WD.Range.InsertAfter(#13);
// WD.Range.InsertAfter(#13);
// WD.Range(Start_Var,End_Var);
// Down_Var:=2;
ex_Var:=wdMove;
// WA.Selection.MoveDown(un_DownVar,Down_Var,ex_Var);
AdoQryCompany.Next;
inc(i);
end;
{ except
WA.Disconnect;
ShowMessage('Please try again!');
abort;
end;}
end;