程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 用Delphi控制Word的輸出

用Delphi控制Word的輸出

編輯:Delphi

 類:應用軟件

  語  種:簡體中文

  編輯器:Delphi6

  平  台:Win9x,Win2k/NT,WinXP

  作品源代碼:http://file.ddvip.com/2008_11/1225675261_ddvip_6293.zip

  軟件或演示: -

  代碼大小: 245.3K

  軟件大小: -

用Delphi控制Word的輸出

  研究了半個小時的結果,不是很完美。

  說明:

  1、cld.dot和cljg.dot是打印模板;

  2、template.res為模板資源文件,不能沒有;

  3、template.rc為未編譯過的資源文件的腳本;

  編譯腳本的命令用brcc32命令,如:

  D:BorlandDelphi6Bin>brcc32 -32 FileFullPath[Like D:StartWordtemplate.rc]

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

  代碼文檔:

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.


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved