Do Delphi Report Output Tool with Word

zhaozj2021-02-11  222

Use Word to do Delphi Report Output Tool Edit: The CHINA ASP author discovered that the contents of the report generally change, but its font format and layout are often changed, and sometimes users do not want to modify the database. Real content and change the contents of the report. If you can solve the former problem with ReportSmith, it is not powerful for the latter, and its interface is in English, no user habits. If both TQREPORT report components in 3.0, both cannot resolve in real time, and must be recompiled after modifying the original code. And Word and Excel can overcome the above shortcomings. Specific implementation is as follows (as an example of Word):

First edit the report format in Word, and rank, replace the data items you want to output, and name it. Here we temporarily assume that there are form fields Item1 and item2 (all text types), save this document as template file EXAMPLE.DOT, then follow the steps:

1) Run Delphi3, add the TDDECLIENTCOV part in a System component set in Form1, named Ddeexample, set it to DDEMANUAL (manual way); set DDeService to '(Winword)'; set serviceApplication to 'WinWord '.

2) Write a custom process to activate Word, as follows: Procedure TFORM1.WordActive (cmds: tstrings); var wordpath: string; begin if (not ddeexample.openlink) THEN {Judging whether the dynamic link} begin if (FindWindow 'OpusApp', NIL) = 0) The begin WordPath: = 'c: / msoffice / winword'; if (WordPath = '') Then ShowMessage ('Chinese Word is not installed or not set, please install the Word Chinese version. ') else begin DdeExample.ServiceApplication: = WordPath ' / Winword.exe '; if (DdeExample.OpenLink) then {if Pat dynamic link macro commands are executed} DdeExample.ExecuteMacroLines (Cmds, False) else ShowMessage (' not start Chinese Language Word ! '); DdeExample.ServiceApplication: =' WinWord.exe '; end; end else begin {if Pat dynamic link macro commands are executed} DdeExample.ExecuteMacroLines (Cmds, False); end; end else DdeExample.ExecuteMacroLines (Cmds, false) ;

Add below the Private declaration area: Procedure ActiveWord (CMDS: Tstrings);

3) add a button to Form1 Button1 written in its onclick event in the following code: procedure TForm1.Button1Click (Sender: TObject); var Cmds: TStringList; {Create Cmds} TempItem1, TempItem2: String; begin cmds: = TStringList. Create; cmds.clear; Tempitem1: = 'data item one'; Tempitem2: = 'data item 2'; with cmds do beg clean; add ('[filenew.template = "eXample.dot"]); {Open Template File EXAMPLE.DOT} Add ('[AppMaximize]'); {Document Maximization} Add ('[SetFormResult "Item1", "' TempiteM1 '"]'); {TempiteM1 is passed to the table single field item1} Add ( '[SetformResult "ITEM2", "' Tempitem2 '"]); {TempiteM2 to the table single field item2} end; WordACTIVE (Ddeexample, CMDS); {Call Custom Process} cmds.Free; {Release CMDS} END To run this program, click Button1, you can find that Word is started, the screen appears: data item one; data item two data items. Finally, everyone can arbitrarily modify the formats and data of this report, as this statement is not related to the specific application.

This example is used in Chinese Word6 or Chinese Word7. Since the Word97 macro command is changed to the Visual Basic statement, if you want to use the Word97 implementation, change its macro command to the corresponding code.

This is a simple example. You can use the Word's macro recording function, admissible more macros (such as automatically generating a table, fill the text, changing font, etc.), and link with various tables of the database, join the CMDS sequentially You can realize the more complex features you require.

转载请注明原文地址:https://www.9cbs.com/read-4245.html

New Post(0)