Several methods of delphi control word -- turn

To judge the degree of difficulty of several methods 

a. Embed Word through the control TOleContainer of Delphi 

This is the simplest Ole embedding, which can directly call the Word document, just use ToleContainer.Run to start

the Word 
document directly. And the Word document started in this way and the Delphi program are a whole (from the interface), but it

has an 
insurmountable shortcoming, that is, the Word document cannot be controlled through Delphi, and the purpose of flexibly manipulating Word cannot be achieved. 

b. Use the Servers control provided by Delphi to call Word, use the properties of Word to 
use the Servers control of Delphi to manipulate Word, Delphi can implement code hints during programming, and generally speaking,

it can  better
achieve Delphi's control of Word, but There are also some Word functions that cannot be called in Delphi (such as

VBA macro code written by yourself). 
And when the function is implemented, it is optional in the VBA code, the parameters must be added when Delphi calls, otherwise, even the compilation will

not pass. 
The Word and Delphi programs started in this way belong to two forms. 
This method can only be used as a reference. 

c. Through the real Com technology, import all the class libraries in the file MSWORD9.OLB in the Office software directory into Delphi


use the Com technology to program and 
use the real Com technology, import the MsWord9.OLD file class library, and then use the Com technology to carry out use. 
It is similar to the Servers control using Delphi as a whole, slightly more troublesome than the Servers control, and has the

same advantages and disadvantages as the Servers control. 

d. Use CreateOleObject to start Word, and then control Word in Ole mode. 
This method is to use CreateOleObjects to call Word, which is actually still Ole, but this method can

truly 
control the Word file and use all the properties of Word, including the VBA macro code written by yourself. 
Compared with Servers control and com technology, this method can really use various properties of Word, which is basically the same as writing

one's own code  in VBA, and the
default code does not need to be used. 
The Word and Delphi programs started in this way belong to two forms. 
The disadvantage is that there is no Delphi code hint when using this method, all exception handling needs to be written by yourself, and there may be

more exploratory knowledge when writing.

[b] Five, the detailed description of Word command macro[/b] 
Word itself contains many command functions, but neither word online help nor MSDN help, there is no

introduction in this regard, so we can only 
rely on our own experiments to explore, The functions of the preliminary detection are as follows: 
Macro Name Explanation Comment 
FileNew New 
FileNewDefault New Blank Document 
FileSaveAs Save As 
FileOpen Open 
FileClose Close 
FilePrint Printing 
FilePrintPreview Print Preview 
ToolsCustomize Customization in Toolbar 
ToolsOptions Tool Options 
ToolsRevisions Highlight revisions 
ToolsReviewRevisions Accept or reject revisions 
ToolsRevisionMarksAccept Accept revisions 
ToolsRevisionMarksReject Reject revisions 
ToolsRevisionMarksToggle Revise 
ToolsMacro Macros 
ToolsRecordMacroToggle Record new macros 
ViewSecurity Security 
ViewVBCode View VB editor environment 
FileTemplates Templates and loadables Item 
ToolsProtectUnprotectDocument Unprotect the document 
InsertHyperlink Insert hyperlink 
EditHyperlink Edit hyperlink 
DeleteHyperlink Delete hyperlink 
EditLinks View, delete link 
EditPasteAsHyperlink Paste hyperlink 
FormatStyle style 
EditBookMark Bookmark 


[b]One, Delphi program starts Word[/b] 
Use CreateOleObjects method to Start Word and call the VBA code. The specific implementation process is as follows: 
First, use GetActiveOleObject('Word.Application') to judge whether there is a Word program in the current memory. If it exists, connect it directly. If there is no Word program, use CreateOleObject('Word.Application') to start Word 

[b] Second, Delphi program New Word document[/b] 
Format: WordDocuments.Add(Template,NewTemplate,DocumentType,Visible) 
Template: Use the name of the template, 
NewTemplate: The type of the new document, True means a template, False means a document 
DocumentType: Document type, default It is a blank document 
Visible: Whether the salvaged window is visible 

Example: Doc_Handle:=Word_Ole.Documents.Add

(Template:='C:\Temlate.dot',NewTemplate:=False); 

[b] Third, Delphi program to open Word document [ /b] 
Format: WordDocuments.Open

(FileName,ConfirmConversions,ReadOnly,PassWordDocument, 


PasswordTemplate,Revent,WritePasswordDocument,WritePassWordTemplate,Format,Encoding,
Visible) 

FileName: Document name (including path) 
Confirmconversions: Whether to display the file conversion dialog 
ReadOnly: Whether to open the document as read-only 
AddToRecentFiles: Whether to add the file to the recent files list at the bottom of the File menu 
PassWordDocument: The password required to open this document 
PasswordTemplate: To open this template Password required when 
Revert: Whether to reopen the document if the document has already been 
written WritePasswordDocument: Save the password required when making changes to the document WritePasswordTemplate: Save the password required 
when making changes to the template 
Format: The file converter required to open the document 
Encoding: Document code page used 
Visible: Whether the window to open the document is visible 

Example: 
Doc_Handle:=Word_Ole.Documents.open(FileName:=Doc_File,ReadOnly:=False, 
AddToRecentFiles:=False); 

[b] Fourth, Delphi program Save Word document[/b] 
Format: WordDocuments.SaveAs(FileName, FileFormat, LockComments, Password, AddToRecentFiles, WritePassword, ReadOnlyRecommended, EmbedTrueTypeFonts, 
SaveNativePictureFormat, SaveFormsData, 
SaveAsAOCELetter) 

FileName: File name. Defaults to the current folder and file name. 
FileFormat The format in which the document is saved. 
LockComments If True, only comments are allowed for this document. 
Password The password to open the document. 
AddToRecentFiles If True, add documents to the list of recent documents in the File menu

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324989169&siteId=291194637