手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

用Delphi控制Word的输出

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
分  类:应用软件
语  种:简体中文
编辑器:Delphi6
平  台:Win9x,Win2k/NT,WinXP
作品源代码: 本地下载
软件或演示: -
代码大小: 245.3K
软件大小: -

研究了半个小时的结果,不是很完美。
说明:
1、cld.dot和cljg.dot是打印模板;
2、template.res为模板资源文件,不能没有;
3、template.rc为未编译过的资源文件的脚本;

编译脚本的命令用brcc32命令,如:
D:\Borland\Delphi6\Bin>brcc32 -32 FileFullPath[Like D:\StartWord\template.rc]

4、template.rc用记事本直接修改就可以了。

李鑫
2003-7-20


代码文档:
unit StartWord;
{
********************************************************************
本例子列举了如何启动Word,并取Word文档中的表格和书签,然后添加值
Author: 李鑫
2003-7-20 于北京
********************************************************************
}

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, Word2000;

type
TForm1 = class(TForm)
Button1: TButton;
CheckBox1: TCheckBox;
ListBox1: TListBox;
wordApp: TWordApplication;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
folderName: String;

implementation

{$R *.dfm}
{$R template.res}

procedure TForm1.Button1Click(Sender: TObject);
var
templateName: OleVariant;
newTemplate: OleVariant;
ItemIndex: OleVariant;
vSelection: Selection;
VBookMark: BookMark;
vTable: Table;
I : Integer;
begin
//构造打印模板文件名全路径
case ListBox1.ItemIndex of
0:
begin
templateName := folderName 'cld.dot';
end;
1:
begin
templateName := folderName 'cljg.dot';
end;
end;

newTemplate := False;

try
wordApp.Connect();
except
MessageDlg('您的计算机上还未安装Microsoft Office Word97或更高的版本!',
mtError, [mbOK], 0);
Abort;
end;

//以指定的模板文件创建新Word文档
wordApp.Documents.AddOld(templateName, newTemplate);
wordApp.Caption := ListBox1.Items.Strings[ListBox1.ItemIndex];

vSelection := wordApp.Selection;

case ListBox1.ItemIndex of
0:
begin
//取文档中的第1张表
vTable := wordApp.ActiveDocument.Tables.Item(1);

vTable.Cell(1, 2).Range.Text := '李鑫 于 北京供电公司';
vTable.Cell(1, 4).Range.Text := '昆明锐祺软件';
vTable.Cell(2, 2).Range.Text := '昆明市白塔路延长线387号星耀大厦1102B座 李鑫(收) 650051 (0871)3126628';
vTable.Cell(3, 2).Range.Text := DateToStr(Now);
vTable.Cell(3, 4).Range.Text := DateToStr(Now);

vTable.Cell(5, 1).Range.Text := '哈哈哈哈哈哈,我也不知道了。';
vTable.Cell(7, 1).Range.Text := '不知道了';
vTable.Cell(9, 2).Range.Text := '不知道';
end;
1:
begin
ItemIndex := 1;
end;
end;

for I := 1 to wordApp.ActiveDocument.Bookmarks.Count do
begin
ItemIndex := I;
//取指定Index的书签
vBookMark := wordApp.ActiveDocument.Bookmarks.Item(ItemIndex);
//ShowMessage(vBookMark.Name IntToStr(wordApp.ActiveDocument.
Bookmarks.Count) 'Now=' IntToStr(i));
if LowerCase(vBookMark.Name) = 'nd' then
begin
vBookMark.Select();
vSelection.InsertAfter('2003');
end;

if LowerCase(vBookMark.Name) = 'bh' then
begin
vBookMark.Select();
vSelection.InsertAfter('1');
end;
end;

wordApp.Visible := CheckBox1.Checked;
wordApp.UserName := '李鑫';
//先最小化
wordApp.WindowState := 2;
//再最大化
wordApp.WindowState := 1;
//打印预览
wordApp.PrintPreview := CheckBox2.Checked;

//立即打印
if CheckBox3.Checked then
wordApp.PrintOutOld;

wordApp.Disconnect();
end;

procedure TForm1.FormCreate(Sender: TObject);
var
templateName: String;
fileStream: TResourceStream;
begin
//获得应用程序所在的目录
folderName := ExtractFilePath(Application.ExeName);

if not FileExists(folderName 'template.res') then
begin
MessageDlg('错误:找不到模板资源文件template.res!', mtError, [mbOK], 0);
Close;
end;

//构造WORD打印模板的文件名全路径
templateName := folderName 'cld.dot';
//获得资源文件里的“处理单”打印模板
fileStream := TResourceStream.Create(hInstance, 'cld', 'WORD');

if not FileExists(templateName) then
begin
//拆离文件
fileStream.SaveToFile(templateName);
//释放
fileStream.Free;
end;

//构造WORD打印模板的文件名全路径
templateName := folderName 'cljg.dot';
//获得资源文件里的“处理结果”打印模板
fileStream := TResourceStream.Create(hInstance, 'cljg', 'WORD');

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!