调用WORD的一个稳当,在固定地方插入来自数据库的文字后再打印。(100分)

Z

zsrobin

Unregistered / Unconfirmed
GUEST, unregistred user!
我有一些通知书,想在固定WORD文档的抬头插入来自数据库的一些文字后,
再用程序统一打印。请问具体操作如何呢? 我写了一些代码:

unit main;

interface

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

type
TForm1 = class(TForm)
WordApplication: TWordApplication;
Button1: TButton;
WordDocument: TWordDocument;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var Template,NewTemplate,ItemIndex:OleVariant;

begin
try
Template := 'd:/normal.dot';
NewTemplate := true;
ItemIndex := 1;

try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';

WordApplication.Documents.Add(Template,NewTemplate);

WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));

WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;


{Insert data}
WordDocument.Range.InsertAfter('大家好');

except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;


end;
 
use bookmark function(VBA).Please search this function in VBA help.
use fieldbyname.asstring replace bookmark.
 
你自己试一试:
1、打开你的模版文档;
2、录制宏;
3、在需要插入的地方插入一些内容;
4、停止录制宏;
5、查看宏内容;
6、将宏内容的程序改成DELPHI的添加到你的程序中。
 
最快的方式的使用
文本替换,速度非常快。
 
前面有过类似的讨论 请查询
http://www.delphibbs.com/delphibbs/dispq.asp?lid=435578
 
使用替换:
//替换
procedure replaceword(ts,td:string);
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant;
begin
FindText := ts;
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindContinue;
Format := False;
ReplaceWith :=td;
Replace := True;
MainForm.WordDocument.Range.Find.Execute( FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace );
end;

将需要的地方输入相应特殊字符,如:<#$name> <#$sex> 在用上面函数替换即可
 
//launch word
procedure TForm1.Button1Click(Sender: TObject);
begin
try
Wordapplication1.Connect;
except
on E: Exception do
begin
Showmessage(E.Message);
Abort;
end;
end;
Wordapplication1.Visible := true;
WordApplication1.Caption := 'Delphi automation';
end;

//open word document
procedure TForm1.Button3Click(Sender: TObject);
var
ItemIndex: OleVariant;
FileName, ConfirmConversions, ReadOnly, AddToRecentFiles,
PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument, WritePasswordTemplate, Format: OleVariant;
begin
//if not Opendialog1.Execute then Exit;

{Open document}
//FileName := Opendialog1.FileName;
FileName := 'E:/About Dephi/Example/ole/word/try.doc';
ConfirmConversions := False;
ReadOnly := False;
AddToRecentFiles := False;
PasswordDocument := '';
PasswordTemplate := '';
Revert := True;
WritePasswordDocument := '';
WritePasswordTemplate := '';
Format := wdOpenFormatDocument;

WordApplication1.Documents.Open(FileName, ConfirmConversions, ReadOnly,
AddToRecentFiles, PasswordDocument, PasswordTemplate, Revert,
WritePasswordDocument,
WritePasswordTemplate, Format);

{Assign WordDocument component}
ItemIndex := 1;
WordDocument1.ConnectTo(WordApplication1.Documents.Item(ItemIndex));

{Turn Spell checking off because it takes a long time if enabled and slows down Winword}
WordApplication1.Options.CheckSpellingAsYouType := False;
WordApplication1.Options.CheckGrammarAsYouType := False;
end;

//replace a specified string
procedure TForm1.Button4Click(Sender: TObject);
var
FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike,
MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace: OleVariant;
begin
FindText := '<#Name>';//待替换字符串
MatchCase := False;
MatchWholeWord := True;
MatchWildcards := False;
MatchSoundsLike := False;
MatchAllWordForms := False;
Forward := True;
Wrap := wdFindStop;
Format := False;
ReplaceWith := 'Delphi';//替换后的字符串
Replace := wdReplaceAll;

WordDocument1.Range.Find.Execute(FindText, MatchCase, MatchWholeWord,
MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward,
Wrap, Format, ReplaceWith, Replace);
end;

//close word
procedure TForm1.Button2Click(Sender: TObject);
var
SaveChanges, OriginalFormat, RouteDocument: OleVariant;
begin
SaveChanges := WdSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication1.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication1.Disconnect;
except
on E: Exception do
begin
Showmessage(E.Message);
WordApplication1.Disconnect;
end;
end;
end;
 
EASY
首先在一个WORD文档中插入书签在你想要打印的位置,如“NAME”
CODE:
VAR
tmpBoolMark:OLEVARIANT;

tmpbookmark:='hospital';
try
Form1..WordApp1.ActiveDocument.Bookmarks.Item(tmpBookMark).Range.
InsertAfter('yourname');
except
end;

可以建立多个书签,使用“try except ”,如果书签不存在可以屏蔽错误
 
delphi中有这样的例子,你可以看一看啊
 
谢谢各位。。。
 
顶部