Directly control Word97 with Delphi4.0
article:
No .: QA003053 Date: June 15, 2000 Last Modified Date: June 15, 2000 Category:
Delphi - Office development
Summary: Friends who have played Powerbuilder must know that the most important technology in PB is its DataWindow technology, with it to design a form, handle data entry, design reports are very convenient. However, Delphi's report support function is far from PB. Delphi's professional version includes QuickReport, but it is a collection of report components authorized by QSD AS (a Norwegian company). Of course, we can also use external report tools such as ReportSmith or Cristal Report. However, the control is slightly complicated, and their integration with Delphi. When we develop the "Document Management System" network version, we have tried different ways when implementing document printing. Our requirements are to print an ordinary document in a document format, and the user can conduct some simple controls for it. Xiao Hui just started using QuickReport to try, especially for some support for some Chinese formats; later Xiao Hui did not make two people, simply wrote print procedures, not relying on design tools, but unfortunately Xiao Hui technology is not home, can't play it when I wrote it, I have to find another way. It just came to the "China Computer News" that day, there is an article on how to use Excel in VB. Xiao Huiyi: Why don't you send data to Word, by Word to complete the editing typesetting? It is said to use OLE automation technology. Xiao Hui is trying, the effect is OK. - Although for a programmer, you have to pick up a person's application in your own program to fully realize your own function, just like a duck in the chicken group, always a little bit. However, due to the completion of the development task, the director came from three from three, although it was a bit a bit of a non-class, Xiao Hui did not care so much. Ok, talk less, see how Xiao Hui is achieved - link: http://www.xiaohui.com Host: Because the original link has been invalid. We are hereby provided: 1. Friends who have played Powerbuilder must know that the most important technology in PB is its DataWindow technology, with it to design a form, handling data entry, design reports are very convenient. However, Delphi's report support function is far from PB. Delphi's professional version includes QuickReport, but it is a collection of report components authorized by QSD AS (a Norwegian company). Of course, we can also use external report tools such as ReportSmith or Cristal Report. However, the control is slightly complicated, and their integration with Delphi. When we develop the "Document Management System" network version, we have tried different ways when implementing document printing. Our requirements are to print an ordinary document in a document format, and the user can conduct some simple controls for it. Xiao Hui just started using QuickReport to try, especially for some support for some Chinese formats; later Xiao Hui did not make two people, simply wrote print procedures, not relying on design tools, but unfortunately Xiao Hui technology is not home, can't play it when I wrote it, I have to find another way. It just came to the "China Computer News" that day, there is an article on how to use Excel in VB. Xiao Huiyi: Why don't you send data to Word, by Word to complete the editing typesetting? It is said to use OLE automation technology. Xiao Hui is trying, the effect is OK.
- Although for a programmer, you have to pick up a person's application in your own program to fully realize your own function, just like a duck in the chicken group, always a little bit. However, due to the completion of the development task, the director came from three from three, although it was a bit a bit of a non-class, Xiao Hui did not care so much. Ok, talk less, see how Xiao Hu is realized - II, the form design said that it is actually very simple. Xiao Hui has made a simple sample program: 1. Set the Font.Name of the form Form1 is the 'Song Body', font.size is 12; 2. Place Lable1-Labe5 TLABLE controls on the form, its CAPTION The attributes are the '文 号', 'Title', 'Collection Unit', 'Text', 'Essay Unit' 3. Place TEDIT, TEDIT, TEDIT, TMEMO, TEDIT, five editing controls on the form, its Name property They are: ed_wenhao, ed_biaoti, ed_shouwendanwei, ed_zhenwen, ed_fawndanwei. 4. Place two TBUTTON controls on the form, the Name attribute is BTN_PrinTtoWord, BTN_QUIT, and the CAPTION attribute is 'print' and 'exit', respectively. Form design format can be referred to herein.
Third, the design of the code block are 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: transmitting data to the word event procedure TForm1.Btn_PrintToWordClick (Sender: TObject ); Var Varword: Variant; // Begin try // when creating Word, Establish OleObject, connect Word97 Varword: = CreateoleObject ('Word.basic'); // 2. Create a new file Varword.FileNew; // 3. set the basic status of Word97 varword.viewZoom75; // Set the display ratio is 75% varword.viewPage; // change to Page Display Mode // 4. Send the information on the current data control to Word97 // 4.1 Send a Data Varword.centerpara; // Come in Varword.Font ('Song Body'); // Set Font Varword.FontSize (14) ; // Set the font size varWord.insert (# 13 # 13 ed_wenhao.text # 13 # 13 # 13); // 4.2 Send title data varWord.Font ('black body'); varWord.Fontsize (16); Varword.insert (ed_biaoti.text # 13); // 4.3 Send a receiving unit data varWord.Leftpara; // Left align varWord.Font ('Song body'); varword.fontsize (14); varword.insert (# 13 ed_shouwendanwei .Text ':
' # 13); // 4.5 Send a body data varWord.FontSize (14); varword.insert (ed_zhenwen.text # 13); // 4.6 Send text data varword.rightpara; // Right align Varword.Fontsize (14) ); Varword.insert (ed_fawndanwei.text # 13); // 5 final setting varWord.startofDocument; // to the text of the first varword.Appmaximize; // Setting the window Maximize Varword.AppShow; // Display Application Except ShowMessage (' Running Microsoft Word failed! '); End; // end of try end; // end: Data Send to Word Event // Start: Window Close Event Procedure TFORM1.BTN_QUITCLICK (Sender: TOBJECT); begin close; End: Window Close Event End. // This is the tail of the main program, notes one, indicating that 1, because it is just a demo example, so there is no connection with the background database, the actual operation can use TDBEDIT, TMEMO control with tdbedit, Instead of tdbmemo control, add controls such as TTABLE, TDATASOSOURCE, and connect to the database. 2, this news does not consider how to make batch print 3, the program is passed under the PWIN97, Delphi 4.0 Professional, Chinese Word97. 4. Due to different versions of Word, Microsoft converts some OLE automation interfaces in each foreign language. If you use other versions of Word, this program is possible to run. 5. You can get the corresponding driver command through the macro command that is summarized in the Word97 Help; or you can click [Tools] menu in Word97 - click [Custom] - Click the [Keyboard] button. You can check it. Word97 is denoted by the word command displayed. Second, the advantages and disadvantages 1, through the OLE automation technology, handle the printing of the official document to Word to complete, for the end user, more convenient. 2. If you modify the data, you cannot pass the main program called it, it is reflected in the database. This is where it is inconvenient. 3, this example is only an application in special cases. To print a large amount of data tag, production form, Xiaohui feels still in a state of articles Source: Xiaohui path.