控制Word
var
tbl : Table;
i,j:integer;
un_Var,ex_Var,cnt_Var:OleVariant;
row_num,col_num:integer;
st:string;
begin
// 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小
WordApplication1.Connect;
WordApplication1.Visible := True;
WordApplication1.Documents.Add(EmptyParam,EmptyParam);
WordDocument1.Connect;
WordApplication1.ActiveWindow.View.Type_:= wdNormalView;
WordApplication1.Selection.Font.Name :='黑体';
WordApplication1.Selection.Font.Size := 16;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication1.Selection.TypeText('昆明市土地信息系统表格输出');
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.Font.Name := '宋体';
WordApplication1.Selection.Font.Size :=12;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication1.Selection.TypeText('日期'+formatdatetime('yyyy"年"mm"月"dd"日"',now));
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;//回车
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
row_num:=table1.RecordCount;
col_num:=table1.Fields.Count;
tbl := WordApplication1.ActiveDocument.Tables.Add(WordApplication1.Selection.Range,row_num+1,Col_num);
un_Var:=wdCharacter;
cnt_Var:=1;
ex_Var:=wdMove;
table1.First;
for j := 0 to Col_num-1 do //标题
begin
st:=table1.Fields.Fields[j].FieldName;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
for i := 0 to row_num-1 do // 行
begin
for j := 0 to Col_num-1 do // 列
begin
st:=table1.Fields.Fields[j].AsString;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
table1.next;
end;
WordApplication1.Selection.TypeText('制表人:阎磊');
WordApplication1.Selection.TypeParagraph;
end;
可以参考以下代码:
procedure PrintReport4(DSR, AJXZ, CFSJ, JBAQ: PChar); stdcall;
var
Word, Doc, Fields:OleVariant;
begin
Word:=CreateOleObject('Word.Application');
Word.Visible:=True;
Word.Documents.Add('C:/Customs/案件呈报表');
Doc := Word.ActiveDocument;
Fields := Doc.FormFields;
Fields.Item('DSR').Result := String(DSR);
Fields.Item('AJXZ').Result := String(AJXZ);
Fields.Item('CFSJ').Result := String(CFSJ);
Fields.Item('JBAQ').Result := String(JBAQ);
end;
其中案件呈报表就是word模版文件。
打开WORD文档的一段程序
uses ComObj ;
procedure TForm1.Button1Click(Sender: TObject);
var
vWord,vDoc,vRange : Variant ;
sText,sReplace : string ;
lReturn : Boolean ;
begin
sText := 'ABCDEFG' ; //原文字串
sReplace := 'GFEDCBA' ; //新字串
vWord := CreateOleObject('Word.Application') ;//创建Word线程
try
//打开要操作的文件
vDoc := vWord.Documents.Open('C:/My Documents/AAAc.Doc');
vDoc.Select ; //选取中整个文档
vRange := vDoc.Range ; //替换范围
lReturn := True ;
while lReturn do
begin //找到并替代成功则返回 True 共11个参数
lReturn := vDoc.Range.Find.Execute(sText,,,,,,,,,sReplace,True) ;
end ;
finally
vDoc.Close(True) ; //关闭文并保存
vWord.Quit(False) ; //退出Word
end ;
end;
例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,OleCtnrs,ComObj;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
ED_WenHao: TEdit;
ED_BiaoTi: TEdit;
ED_ShouWenDanWei: TEdit;
ED_ZhenWen: TMemo;
ED_FaWenDanWei: TEdit;
Btn_PrintToWord: TButton;
Btn_Quit: TButton;
procedure Btn_PrintToWordClick(Sender: TObject);
procedure Btn_QuitClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//开始:数据发送到 word事件
procedure TForm1.Btn_PrintToWordClick(Sender: TObject);
vAR
VarWord: Variant;// 创建 WORD时所用
begin
try
// 1. 建立 OleObject,连接 word97
VarWord:=CreateOleObject('word.basic');
// 2. 建立 Word97的新文件
VarWord.FileNew;
// 3. 设置 Word97的基本状态
VarWord.ViewZoom75; //设置显示比例为 75%
VarWord.ViewPage; //改为页面显示方式
// 4. 将当前数据控件上的信息发送至 Word97
// 4.1 发送文号数据
VarWord.CenterPara; //居中
Varword.font('宋体 '); //设置字体
VarWord.FontSize(14); //设置字号
varword.insert(#13+#13+ ED_WenHao.Text+#13+#13+#13);
// 4.2 发送标题数据
VarWord.font('黑体 ');
VarWord.Fontsize(16);
VarWord.insert( ED_BiaoTi.text+#13);
// 4.3 发送收文单位数据
VarWord.LeftPara; //左对齐
VarWord.Font('宋体 ');
VarWord.fontSize(14);
VarWord.Insert(#13+ ED_ShouWenDanWei.Text+': '+#13);
// 4.5 发送正文数据
VarWord.fontSize(14);
VarWord.Insert( ED_ZhenWen.Text+#13);
// 4.6 发送发文单位数据
VarWord.RightPara; //右对齐
VarWord.fontSize(14);
VarWord.Insert( ED_FaWenDanWei.Text+#13);
// 5 最后设置
VarWord.StartOfdocument; //到文首
VarWord.AppMaxiMize; //设置窗口最大化
VarWord.AppShow; //显示应用程序
except
showmessage('运行 Microsoft Word 失败! ');
end; //end of try
end;
//end:数据发送到 word事件
//开始:窗口关闭事件
procedure TForm1.Btn_QuitClick(Sender: TObject);
begin
close;
end;
//End:窗口关闭事件
end.
// 这是主程序的尾部