Delphi call word

xiaoxiao2021-03-06  55

Delphi Word = Database official document processing

Delphi is good at MIS development of database classes, but there is a bit force for OA. However, with Microsoft's COM technology matures, now ordinary Windows applications have been seamless with Office 97,

Especially in Delphi 5, a set of Servers components is also simplified program development.

I recently contacted a user case, and use Delphi to control Word to make a contract management program.

The office personnel will write the text of the contract based on the business needs, but in the user name, the product name and other changes.

Fill in the specified tag string, then replace the actual data in the database with Delphi.

Word, and finally let Word print out the contract.

Delphi comes with a simple Word example, but functions are too simple. Look for the description of VBA, then

Delphi's VCL prepared the following code to realize 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 your Doc file, modify the contents 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;

Let Word open a specified file, you need to place OpenDialog first, then call WordApplication.Documents.Open:

VAR

ItemIndex: olevariant;

FileName, Confirmconversions, Readonly, AddtorecentFiles,

PasswordDocument, PasswordTemplate, Revert,

WritepasswordDocument, WritePasswordTemplate, Format: Olevariant;

Begin

IF not dlgopen.execute the

EXIT;

{Open Document}

Filename: = DLGOPEN.FILENAME;

CONFIRMCONVERSIS: = 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 Slows Down Winword}

WordApplication.Options.checkspellingsyOoutype: = false;

WordApplication.Options.checkgrammarasyoutype: = false;

END;

Let the Word replace the marker string to use WordDocument.Range.Find.execute, here is replaced with Delphi <#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 codes completed the basic functions of document management, and then combined it with the database, they can develop one.

A product similar to Lotus Notes.

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

New Post(0)