In practical applications, the author found that the content of the report rarely changes, but its font format and layout often change, and sometimes users do not want to modify the actual content of the database but want to change the report content for certain needs. If you use ReportSmith, you can solve the former problem, but you can't do anything about the latter, and its interface is in English, which is not suitable for users. If you use the TQReport report component in 3.0, neither can be solved in real time, and the original code must be modified and recompiled before it can be used. Using Word and Excel can completely overcome the above shortcomings. The specific implementation is as follows (taking Word implementation as an example):
First use Word to edit the report format, lay out the format, replace the data items to be output with form fields, and name them. Here we temporarily assume that there are form fields Item1 and Item2 (both text type), save this document as a template file Example.dot, and then proceed as follows:
1) Run Delphi3, add a TDdeClientCov component in the System component set to Form1, name it DdeExample, set its ConnectMode to ddeManual (manual mode); 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 {determine whether it is dynamically linked}
begin
if(FindWindow('OpusApp', nil)=0) then
begin
WordPath := 'C:msofficewinword';
if(WordPath=') then
ShowMessage('Chinese Word is not installed or the path is not set, please install and set the Chinese version of Word.')
else begin
DdeExample.ServiceApplication := WordPath+'Winword.exe';
if(DdeExample.OpenLink) then {if the macro command has been dynamically linked}
DdeExample.ExecuteMacroLines(Cmds,False)
else
ShowMessage('Unable to start the Chinese version of Word!');
DdeExample.ServiceApplication := 'WinWord.exe';
end;
end
else begin{if the macro command has been dynamically linked}
DdeExample.ExecuteMacroLines(Cmds,False);
end;
end
else
DdeExample.ExecuteMacroLines(Cmds,false);
end;
Add the following to the private declaration area:
procedure ActiveWord(Cmds: TStrings);
3) Add a button Button1 to Form1 and write the following code in its onclick event:
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 two';
with Cmds do
begin
Clear;
Add('[FileNew.Template ="Example.Dot″]');{Open template file Example.Dot}
Add('[AppMaximize]');{Document Maximize}
Add('[SetFormResult "Item1",″'+TempItem1+'″]');{Pass data TempItem1 to form field Item1}
Add('[SetFormResult "Item2","'+TempItem2+'"]);{Pass data TempItem2 to form field Item2}
end;
WordActive(DdeExample,Cmds);{Call custom process}
Cmds.Free;{Release Cmds}
end;
Run this program, click Button1, you can find that Word is started, and two data items appear on the screen: data item one; data item two. Finally, you can modify the format and data of this report at will, because this report has nothing to do with the specific application.
In this example, Chinese Word6 or Chinese Word7 is used. Since the macro commands of Word97 have been changed into Visual Basic statements, if you want to use Word97 to implement it, please change the macro commands into the corresponding codes.
This is a simple example. You can use the macro recording function of Word to record more macros (such as automatically generating tables, filling text, changing fonts and other macro commands), and connect them with various tables in the database, and add them to Cmds in turn. You can achieve more complex functions you require.