Use Delphi4.0 to directly control Word97
No.: QA003053
Creation date: June 15, 2000 Last modification date: June 15, 2000
Category:
article :
Summary: Friends who have played PowerBuilder must know that the most important technology in PB is its DataWindow technology. It is very convenient to use it to design forms, process data entry, and design reports. However, Delphi's report support function is far less than that of PB. The professional version of Delphi includes QuickReport, but it is a collection of report components licensed by QSD AS (a Norwegian company). Of course, we can also use external reporting tools, such as ReportSmith or Cristal Report. However, the controls are a bit complex and there is a lack of integration between them and Delphi.
When we were developing the online version of the "Official Document Management System", we tried different methods when printing official documents. Our requirement is to print an ordinary official document in official document format, and the user can perform some simple control on its typesetting. Xiaohui first tried using QuickReport, but the effect was very unsatisfactory, especially in terms of support for some Chinese formats. Later, Xiaohui was so obsessed with it that he simply wrote the printing program by hand without relying on design tools, which was a pity. Xiaohui's skills were not good enough, and he couldn't continue playing when he was halfway through writing, so he had to find another way. It happened that a copy of "China Computer News" came that day, and there was an article on how to use Excel in VB. Xiaohui thought: Why not use Delphi to send the data to Word, and let Word complete the editing and typesetting work? To put it bluntly, it uses OLE automation technology.
Xiaohui gave it a try and the effect turned out to be okay. ——Although for a programmer, the program he writes must be connected to someone else's application to fully realize his own functions, it is like inserting a duck into a flock of chickens, and there is always something in his heart. However, due to the tight development tasks, the director came to urge him every three or four times. Although it ended up being a bit nondescript, Xiaohui couldn't care less. Okay, without further ado, let’s take a look at how Xiaohui achieved it——
Link: http://www.xiaohui.com
Moderator: Because the original link has expired. We provide the original text here:
1. Preface
Friends who have played PowerBuilder must know that the most important technology in PB is its DataWindow technology. It is very convenient to use it to design forms, process data entry, and design reports. However, Delphi's report support function is far less than that of PB. The professional version of Delphi includes QuickReport, but it is a collection of report components licensed by QSD AS (a Norwegian company). Of course, we can also use external reporting tools, such as ReportSmith or Cristal Report. However, the controls are a bit complex and there is a lack of integration between them and Delphi.
When we were developing the online version of the "Official Document Management System", we tried different methods when printing official documents. Our requirement is to print an ordinary official document in official document format, and the user can perform some simple control on its typesetting. Xiaohui first tried using QuickReport, but the effect was very unsatisfactory, especially in terms of support for some Chinese formats. Later, Xiaohui was so obsessed with it that he simply wrote the printing program by hand without relying on design tools, which was a pity. Xiaohui's skills were not good enough, and he couldn't continue playing when he was halfway through writing, so he had to find another way. It happened that a copy of "China Computer News" came that day, and there was an article on how to use Excel in VB. Xiaohui thought: Why not use Delphi to send the data to Word, and let Word complete the editing and typesetting work? To put it bluntly, it uses OLE automation technology.
Xiaohui gave it a try and the effect turned out to be okay. ——Although for a programmer, the program he writes must be connected to someone else's application to fully realize his own functions, it is like inserting a duck into a flock of chickens, and there is always something in his heart. However, due to the tight development tasks, the director came to urge him every three or four times. Although it ended up being a bit nondescript, Xiaohui couldn't care less. Okay, without further ado, let’s take a look at how Xiaohui achieved it——
2. Form design
To put it bluntly, it's actually very simple. Xiaohui made a simple sample program here:
1. Set the Font.name of Form1 to 'Songti' and Font.size to 12;
2. Place five tLable controls, label1-labe5, in sequence on the form. Their caption properties are 'document number', 'title', 'receiving unit', 'text', and 'sending unit'.
3. Place five edit controls: tEdit, tEdit, tEdit, tMemo, and tEdit in sequence on the form. Their name attributes are: ED_WenHao, ED_BiaoTi, ED_ShouWenDanWei, ED_ZhenWen, and ED_FaWenDanWei respectively.
4. Place two tButton controls in sequence on the form. Their name attributes are Btn_PRintToWord and btn_Quit, and their Caption attributes are 'Print' and 'Exit' respectively.
For the form design format, please refer to the picture attached at the end of this article.
3. Code design
The program segment is as follows:
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}
//Start: Data is sent to word event
procedure TForm1.Btn_PrintToWordClick(Sender: TObject);
vAR
VarWord: Variant;// used when creating WORD
begin
try
// 1. Create OleObject and connect word97
VarWord:=CreateOleObject('word.basic');
// 2. Create a new file for Word97
VarWord.FileNew;
// 3. Set the basic status of Word97
VarWord.ViewZoom75; //Set the display ratio to 75%
VarWord.ViewPage; //Change to page display mode
// 4. Send the information on the current data control to Word97
// 4.1 Send document number data
VarWord.CenterPara; //Centered
Varword.font('宋体'); //Set the font
VarWord.FontSize(14); //Set the font size
varword.insert(#13+#13+ ED_WenHao.Text+#13+#13+#13);
// 4.2 Send header data
VarWord.font('HeiBi');
VarWord.Fontsize(16);
VarWord.insert( ED_BiaoTi.text+#13);
// 4.3 Send the receiving unit data
VarWord.LeftPara; //Left aligned
VarWord.Font('宋体');
VarWord.fontSize(14);
VarWord.Insert(#13+ ED_ShouWenDanWei.Text+':'+#13);
// 4.5 Send text data
VarWord.fontSize(14);
VarWord.Insert( ED_ZhenWen.Text+#13);
// 4.6 Send sending unit data
VarWord.RightPara; //right alignment
VarWord.fontSize(14);
VarWord.Insert( ED_FaWenDanWei.Text+#13);
// 5 final settings
VarWord.StartOfdocument; //Go to the beginning of the document
VarWord.AppMaxiMize; //Set the window to maximize
VarWord.AppShow; //Show application
except
showmessage('Failed to run Microsoft Word!');
end; //end of try
end;
//end: data sent to word event
//Start: window closing event
procedure TForm1.Btn_QuitClick(Sender: TObject);
begin
close;
end;
//End: window closing event
end.
// This is the end of the main program
4. Notes
1. Description
1. Because it is just a demonstration example, it is not connected to the background database. In actual operation, the corresponding tEdit and tMemo controls can be replaced with tDBEdit and tDBMemo controls, and tTable, tDataSource and other controls can be added to connect to the database.
2. This guide does not consider how to batch print.
3. The program is passed under Pwin97, Delphi 4.0 Professional Edition, and Chinese Word97.
4. Since there are different versions of Word, Microsoft has converted some OLE automation interfaces in each foreign language. If you use other versions of Word, this program may run incorrectly.
5. You can get the corresponding driver commands through the macro commands summarized in the word97 help; or you can click the [Tools] menu under Word97 - click [Customize] - click the [Keyboard] button to view Word commands displayed in categories in Word97.
2. Advantages and Disadvantages
1. Through OLE automation technology, the typesetting and printing work of official documents is handed over to Word, which makes the control more convenient for the end user.
2. In Word97, if the data is modified, it cannot be sent back to the main program that called it and reflected in the database. That's the inconvenience of it.
3. This example is just an application under special circumstances. If you want to print a large number of data labels and make tables, Xiaohui thinks it is more flexible to use reports.
Article source: Xiaohui’s journey as a programmer .