捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  沪ICP备05001939号 DELPHI盒子 | 盒子论坛 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 论坛检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
盒子资源分类
全部展开 - 全部合拢
用Delphi控制Word的输出
关键字:控制 Word 输出 模板 OLE
来 自:原创
平 台:Win9x,Win2k/NT,WinXP 下载所需:0 火柴
深浅度:中级 完成时间:2003/7/20
发布者:jacky_zz 发布时间:2003/8/31
编辑器:DELPHI6 语  种:简体中文
分 类:应用软件 下载浏览:4650/33689
加入到我的收藏
下载错误报错
登陆以后才能下载
 用户名:
 密 码:
自动登陆(30天有效)
图片如果打不开,说明流量不够了,请稍候下载……
研究了半个小时的结果,不是很完美。
说明:
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');
  if not FileExists(templateName) then
    begin
      //拆离文件
      fileStream.SaveToFile(templateName);
      //释放
      fileStream.Free;
    end;

  ListBox1.Items.Add('处理单');
  ListBox1.Items.Add('处理结果');
  ListBox1.Selected[0] := True;
end;

end.
Google
 
本站原创作品,未经作者许可,严禁任何方式转载;转载作品,如果侵犯了您的权益,请联系我们
龙脉加密锁 15元起 Grid++Report 报表 申请支付@网
 相关文章
Delphi控制Excel的重要属性和方法
sys 2004/9/18 下+435/浏+31544 评+9
用Delphi控制Word的输出
jacky_zz 2003/8/31 下+4650/浏+33690 评+20
利用Variant变量用DELPHI操作EXCEL
mantousoft 2003/8/30 下+3813/浏+27762 评+8
相关评论
共有评论20条 当前显示最后6条评论
sxwy 2006/10/28 9:20:00
其实改为WORDSELECT也无效.
genuine_6205 2007/8/10 9:28:23
非常感谢
merry_bip 2008/1/6 11:59:23
Selection 没有定义,什么意思
merry_bip 2008/1/6 12:18:02
这个正是我要的,非常的感谢!
lenic 2009/6/2 18:11:56
我的是D7 + Office 2003,编译的时候Selection出错,改为WordSelection类型的就没问题了。我D7安装的时候选的是Office XP,不知道是不是和这个有关……
ibasic 2016/1/10 0:06:32
D7+office2010 测试OK,出错的地方改为:Selection--->WordSelection

非常感谢盒子,帮你点广告了
我要发表评论 查看全部评论
 
  DELPHI盒子版权所有 1999-2023 V4.01 粤ICP备10103342号-1 更新RSS列表