Delphi is good at developing database-class MIS, but it cannot help OA. However, with the maturity of Microsoft's COM technology, common Windows applications can now be seamlessly integrated with office97, especially the provision of a set of servers components in Delphi 5, which simplifies program development.
I recently came into contact with a user case and used Delphi to control word for a contract management program. The Office staff first write the contract text according to the business needs, but fill in the specified tag string in the location where the user name, product name, and other changes, then, use Delphi to replace the actual data in the database with the text in the word, and finally let the word print out the contract.
Delphi comes with a simple word example, but the function is too simple. By searching the descriptions of VBA and comparing with Delphi's VCL, the following code is compiled to implement the basic document management function.
Use the following code to start word:
Begin
Try
Wordapplication. Connect;
Except
Messagedlg ('word may not be installed', mterror, [mbok], 0 );
Abort;
End;
Wordapplication. Visible: = true;
Wordapplication. Caption: = 'delphi automation ';
End;
Use the following code to disable word. To save the DOC file, modify the content of the savechanges variable:
VaR
Savechanges, originalformat, routedocument: olevariant;
Begin
Savechanges: = wddonotsavechanges;
Originalformat: = unassigned;
Routedocument: = unassigned;
Try
Wordapplication. Quit (savechanges, originalformat, routedocument );
Wordapplication. Disconnect;
Except
On E: exception do
Begin
Showmessage (E. Message );
Wordapplication. Disconnect;
End;
End;
End;
To open a specified file in word, you must first place opendialog and then call wordapplication. Documents. open:
VaR
Itemindex: olevariant;
Filename, confirmconversions, readonly, addtorecentfiles,
Passworddocument, passwordtemplate, revert,
Writepassworddocument, writepasswordtemplate, format: olevariant;
Begin
If not dlgopen. Execute then
Exit;
... {Open document}
Filename: = dlgopen. filename;
Confirmconversions: = false;
Readonly: = false;
Addtorecentfiles: = false;
Passworddocument: = '';
Passwordtemplate: = '';
Revert: = true;
Writepassworddocument: = '';
Writepasswordtemplate: = '';
Format: = wdopenformatdocument;
Wordapplication. Documents. Open (filename, confirmconversions,
Readonly, addtorecentfiles, passworddocument, passwordtemplate,
Revert, writepassworddocument, writepasswordtemplate, format );
... {Assign worddocument component}
Itemindex: = 1;
Worddocument. connectto (wordapplication. Documents. Item (itemindex ));
... {Turn spell checking of because it takes a long time if enabled and slowsdown winword}
Wordapplication. Options. checkspellingasyoutype: = false;
Wordapplication. Options. checkgrammarasyoutype: = false;
End;
When we select the XP style to use wordxp when installing Delphi, the code here needs to be changed
VaR itemindex: olevariant;
Filename, confirmconversions, readonly, addtorecentfiles, passworddocument, passwordtemplate, revert: olevariant;
Writepassworddocument, writepasswordtemplate, format: olevariant;
Encoding, visible, openandrepair, documentdirection, noencodingdialog: olevariant;
Begin
If not dlgopen. Execute then
Exit;
//... {Open document}
Filename: = dlgopen. filename;
Confirmconversions: = false;
Readonly: = false;
Addtorecentfiles: = false;
Passworddocument: = '';
Passwordtemplate: = '';
Revert: = true;
Writepassworddocument: = '';
Writepasswordtemplate: = '';
Format: = wdformatdocument;
// Encoding
Visible: = true;
Wordapplication1.disconnect;
Wordapplication1.documents. Open (filename, confirmconversions,
Readonly, addtorecentfiles,
Passworddocument, passwordtemplate,
Revert, writepassworddocument, writepasswordtemplate, format,
Encoding, visible, openandrepair, documentdirection, noencodingdialog );
Wordapplication1.visible: = true;
... {Assign worddocument component}
Itemindex: = 1;
Worddocument1.connectto (wordapplication1.documents. Item (itemindex ));
... {Turn spell checking of because it takes a long time if enabled and slows down winword}
Wordapplication1.options. checkspellingasyoutype: = false;
Wordapplication1.options. checkgrammarasyoutype: = false;
End;
Use worddocument. range. Find. Execute to replace the markup string with word. Here, use Delphi to replace <# NAME>:
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: = wdfindcontinue;
Format: = false;
Replacewith: = 'delphi ';
Replace: = true;
Worddocument. range. Find. Execute (findtext, matchcase, matchwholeword,
Matchwildcards, matchsoundslike, matchallwordforms, forward,
Wrap, format, replacewith, replace );
End;
The above four sections of code complete the basic functions of document management, and then combine it with the database, you can develop a product similar to Lotus Notes.