Implement Word 97 automation with C ++ Builder

zhaozj2021-02-11  177

Implementation of Word 97 Automation in Jiangxi Nanchang Huadong Communication Institute of Control Information Engineering (330013) Liu Zhuxiong

---- To control the Word 97 operation in the application, you must first include file comoBj.hpp while preparing an automated client program, writing #include. The developer of programming tool C Builder is generally packaged in the ability to call Word 97 automation to several OLE Object Class functions. Application designers can easily use them.

* Set the properties of the OLE object: Olepropertyget (PropName) OlepropertySet where value is any value that can be converted to a Variant type.

* Method for calling the OLE object: OLEPROCEDURE (OLEPROCNAME, [VAL, ...]) OLEFUNCTION (OLEFUNCNAME, [VAL, ...]) where VAL is any value that can be converted to a Variant type.

---- You can return to its child object by Olefunction (OlefuncName, [Val, ...]) parent object, the syntax is: sub-object name = parent pair name. Olefunction (OlefuncName, VAL). Moreover, it is possible to combine the parent object to returns an object of the lower object from the parent object.

---- C Builder Using OLE Automation Control Word 97, you must master Word 97's automation objects and object methods and properties about Word in the Microsoft Word Visual Basic Help file. The Word object represents an element of a word, such as documentation, paragraph, bookmark, or a single character. A collection is an object that contains several other objects, usually these objects belong to the same type, for example, in a collection object, you can include all the bookmark objects in the document. By using properties and methods, you can modify a single object or modify the entire object collection. Attributes are a characteristic of an object or one aspect of the object operation. For example, document properties contain names, content, save status, and whether to enable revisions. To change the properties of an object, you can modify the value of the property. The method is the action that the object can be performed.

---- There are two automation objects representing the Word 97 application: Word.Application and Word.basic, which can allow them to be converted between the WordBasic attributes through the Application object. You can start the Word and get the handle in C Builder, which is assigned to the Variant variable. If there are two Variant type variables V1 and V2, WordBasic is the properties of the Application object: v1 = createoleObject ("word.Application"); v2 = v1.olefunction ("wordbasic").

---- The following uses a simple example, specifically explains how to implement the Word 97 automation function in C Builder, which is to open or create a new document, and send text to the current activity, insert bitmap and drawing straight lines. Under the C Builder Integration Development Environment IDE, establish a project Project1, which contains unit files unit1.cpp in Form1 (its CAPTION OLE Automation). Form1 OpenDialog1 objects of Form1 are set to Word documents or bitmaps. "COMOBJ.HPP" must be included in the header file of Unit1.cpp. The code is as follows: The header file unit1.h code is:

#Ifndef unit1H # Define unit1H // ------------- # clude #include #include #include #include #include #include // ------------- class TForm1: public TForm {__ published: // IDE-managed Components TButton * Button1; TOpenDialog * OpenDialog1; TButton * Button2; TMemo * Memo1; TBevel * Bevel1; TButton * Button3; TLabel * Label1; TButton * Button5; TBevel * Bevel2; TLabel * Label2; TLabel * Label3; void __fastcall Button1Click (TObject * Sender); void __fastcall Button3Click (TObject * Sender); void __fastcall Button2Click (TObject * Sender); void __fastcall Button5Click (TObject * Sender); private: // User declarationspublic : // User declarations Variant V, Vdocuments, Vdocument1, Vdocument2, Vactive; / * define a Variant global // variables, respectively, refer to the Application object, ActiveDocument Documents V of the object, Vdocuments Document object (Vdocument1, Vdocument2) and the V Object * / int Tag; // Used to record the number of times of button3 clicked __fastcall tform1 (tComponent * Owner); variant __fastcall getWordObject (); // Get Word 97 Automation Object Function VOID __FASTCALL Add (int i); // Edit the function} of the paragraph text specified by the current event document}; // ------------- Extern package tform1 * form1; // ---------- --- # EndifUnit1.cpp file code is: #include #

Pragma HDRSTOP # include "unit1.h" #include // ------------- # pragma package (smart_init) #pragma resource "* .dfm" TFORM1 * FORM1; // ---- ---------__ fastcall tform1 :: tform1: tform (outner) {tag = 1; // / TAG's initial value is 1, which will increment with the number of click times of Button3} / / ------------- Variant __fastcall tform1 :: getWordObject () {variant obj1; ansistring appname = "word.application"; hWnd hprevapp = :: FindWindow (Null, "Microsoft Word"); If (! hprevapp) {Obj1 = creteoleObject (appname); // Word does not start, start it returns an automation object} else obj1 = getActiveoleObject (appname); / / Otherwise returns an instance automation object Obj1.olePropertySet ("Visible ", true); RETURN OBJ1;} void __fastcall tform1 :: button1click (TOBJECT * Sender) {INT J; v = getWordObject (); // Get Word 97 Automation Object ApplicationVdocuments = V.OLEFUNCTION (" Documents "); // (OpenDialog1-> Execute ()) // use documents open method to open the object acquired through the Application documents if the object file, and returns the document object Vdocument1 = Vdocuments.OleFunction ( "open", OpenDialog1-> FileName); j = Vdocument1.OleFunction ("ComputeStatistics", 2); // Calculate the number of pages that open the document Label1-> CAPTION = "File" VDocument 1.olePropertyget ("Name") "page is:" INTTOSTR (j);} // ------------- void __fastcall tform1 :: button3click (TOBJECT *

Sender) {INT I, J; I = Tag; VACTIVE = V.OLEFUNCTION ("ActiveDocument"); // Get Application's ActiveDocument Object J = VACTIVE.OLEFUNCTION ("ComputeStatistics", 4); // Calculate the paragraph of the current document The number of //'s paragraphs set object increases a Vactive.Olefunction ("paragraphs"). OLEFUNCTION ("add"); i = j i; // Current document edited paragraph serial number add (i); // call Edit Current Activity Document specified paragraph text function memo1-> clear (); // clear memo1 content tag = tag 1;} // -------------- void __fastcall tform1 :: button2click (TOBJECT * Sender) {v = getWordObject (); vDocuments = v.olefunction (""); vDocument2 = vdocuments.olefunction ("add"); // Using Documents object Add Document VDocument2.olePropertySet ("Password", "12345 "); // Set the password of the new document} // ------------- VOID __FASTCALL TFORM1 :: Add (int i) {variant V1; // Set the content of the edited segment, Font size, color and whether it blocks V1 = ("paragraphs"). OLEFUNCTION ("ITEM", I)). OLEFUNCTION ("Range"); (v1.olefunction ("font")). OlepropertySet ("Italic", true); ("font")). OLEPROPERTYSET ("SIZE", 18); (V1.OLEFUNCTION ("font")). OlepropertySet ("ColorIndex", 6); (V1 . Kelefunction ("font")). OlepropertySet ("size", 18); v1.olepropertyset ("text", memo1-> text );} // ------------- void __fastcall tform1 :: button5click (TOBJECT * Sender) {variant vshape, vactive1, vline; display = v.olefunction ("ActiveDocument"); vshape = Vactive1.olefunction ("shapes");

// Get the Shapes object // of the current active document // Use the AddLine method of the Shapes object to normalize the straight line = vshape.olefunction ("AddLine", 90, 80, 400, 8); if (OpenDialog1-> execute ()) // Use Shapes objects The AddPicture method is inserted into the specified bitmap vshape.olefunction ("AddPicture", OpenDialog1 -> FileName, 50, 50, 80, 100);} ---- This program compiled in C Builder 3, running the application first must get a document, Then you can edit the document.

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

New Post(0)