Sun Li Delphi is good at MIS development of database class, but it is not a bit for OA. However, as Microsoft's COM technology is gradually mature, the normal Windows applications are now seamless with Office 97, especially in Delphi 5, providing a set of Servers components, but also simplifies program development. I recently contacted a user case, and use Delphi to control Word to make a contract management program. According to the service needs, the office person must write the text of the contract, but fill in the specified tag string in the change of the user name, product name and other changes, and then replace the actual data in the database by Delphi, let Word Print out the contract. Delphi comes with a simple Word example, but functions are too simple. By looking for VBA's instructions, the following code has been prepared by finding the VCL of Delphi, and implements basic document management functions. Use the following code when starting 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; Turn off Word with the following code.
If you want to save Doc file, modify the content SaveChanges variables: 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; Word to open a specific file, you need to place the OpenDialog, 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: = wdOpenFormatDocume nt; 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 slows down Winword} WordApplication.Options.CheckSpellingAsYouType: = False; WordApplication.Options.CheckGrammarAsYouType: = False; end; Word to replace the marker string to use WordDocument .Range.find.execute, here is replaced with Delphi <#Name>: