Realize Word 97 Self-activation with BCB

zhaozj2021-02-17  87

---- To control the operation of Word 97 in the application program, the first must first enable its header to include Comobj.HPP when preparing a self-enabling customer program, write #include. The process of programming tool C Builder puts the function of calling Word 97 self-movement to a few OLE Object Class functions, and the procedure should be used to use them in place.

* Setting the properties of the OLE object: OLEPROPERTYGET (PROPNAME) OLEPROPERTYSET (PROPNAME, VALUE) The value in which Value can be converted to a Variant type value

* Method for calling the OLE paginity: Oleprocedure (OleprocName, [VAL, ...]) olefunction (olefuncname, [val, ...]) VAL is the value that can be converted to Variant type

---- You can return to its child object with the parent pair of olefunction (olefuncname, [val, ...]), the syntax is: child object name = parent pair name. OLEFUNCNAME, VAL). And and can group the object of the lower layer of the lower layer from the parent object from the parent pair.

---- C Builder Use OLE Automatic Control Word 97, must hold the Word 97's automatic objective and property method and attributes of the Microsoft Wordvisual Basic Help documents. Word object represents a word of a word, such as document, paragraph, bookmark or a single character. The collection is an object that contains his several pairs of objects. It is common to these objects that belong to the same type. For example, a set of objects can be included in all bookmarks in the document. Change a single object with a single object, can also modify the entire object collection. The property is a characteristic of an object or a party that is active. For example, the document property package contains name, content, preservation state, and whether it is amended. To change the properties of an object, you can modify the value of the property. The method is the action that can be taken.

---- Generation of the Word 97 Application Program There are two: Word.Application and Word.Basic, which can allow the conversion assignment between the Application objects. In CreateoleObject ("...") in C Builder, you can enable the handle to assign it to the Variant variable. If there are two Variant type variables V1 and V2, WordBasic is the properties of Application objects: v1 = createoleObject ("word.application"); v2 = v1.olefunction ("wordbasic"). ---- Here, through 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 line, etc. Under the C Builder, it is set up a project of Project1, which includes the unit document unit1.cpp containing Form1 (its CAPTION OLE Automation). The Filter of the form of form1 OpenDialog1 is set to the Word document or bitmap. The header of Unit1.cpp must be included in "COMOBJ.HPP".

The code is as follows: The header file unit1.h code is: #ifndef unit1h #define unit1h // - - - - - #include #include #include #include #include #include #include #include #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, the object V of Documents, Vdocuments of Document object (vDocument1, vdocument2), and V's ActiveDocument object * / int tag; // is used to record Button3 clicks __fastcal l TForm1 (tComponent * Owner; variant __fastcall getwordObject (); // Get Word 97 Automation Object Function VoID __FastCall Add (INT i); // Edit the function}; // - - - - - - - - - - -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 (tComponent * Owner: TForm (Owner) {tag = 1; // The initial value of the tag is 1, which will increment with the number of button3 clicks} // - - - - - - - - - -variant __fastcall tform1 :: getWordObject () {variant obj1; Ansistring Appname = "word.application"; hwnd hprevapp = :: FindWindow (NULL, "Microsoft Word"); if (! Hprevapp) {Obj1 = createoleObject (appname); file: // Word does not start starting it to return an automation object } else Obj1 = GetActiveOleObject (AppName); // otherwise automation object instance Obj1.OlePropertySet ( "Visible", true) is running; return Obj1;} void __fastcall TForm1 :: Button1Click (TObject * Sender) {int j; V = GetWordObject (); // Get Word 97 Automation Object ApplicationVDocuments = V.OLEFUNCTION ("Documents"); // Get the Documents object if (OpenDialog1 -> execute ()) // via Application (OpenDialog1-> Execute ()) // Object of Documents PEN method opens the document and returns Document object vdocument1 = vdocuments.olefunction ("open", OpenDialog1 -> filename); j = vDocument1.olefunction ("ComputeStatistics", 2);

// Calculate the number of pages of the open document label1 -> CAPTION = "File" vdocument1.olePropertyget ("Name" "number of pages is:" INTTOSTR (j);} // - - - - - - - - - -void __fastcall TForm1 :: Button3Click (TObject * Sender) {int i, j; i = tag; Vactive = V.OleFunction ( "ActiveDocument"); // Get the Application Object ActiveDocument j = Vactive.OleFunction ( "ComputeStatistics ", 4); // Calculate the paragraGraphs collection object of the current document to add a VACTIVE.OLEFUNCTION (" paragraphs "). OLEFUNCTION (" add "); i = j i; // The current document edited Paragraph serial number add (i); // Call the function of the paragraph text specified by the editing of the current activity document Memo1 -> Clear (); // Clear the contents of Memo1 tag = tag 1;} // - - - - - - - - - - - - VOID __FASTCALL TFORM1 :: Button2Click (TOBJECT * Sender) {v = getWordObject (); vDocuments = v.olefunction (""); vDocument2 = vDocuments.olefunction ("add"); // uses Documents object Add method new document vDocument2.olePropertySet ("Password", "12345"); // Set the password of the new document} / / - - - - - - - - - - - void __fastcall TFORM1 :: Add (Int i ) {Variant v1; // Set the content, font size, color and whether it block V1 = ((Vactive.OLEFUNC) TI1 ("ITEM", I)). OLEFUNCTION ("Range"); ("font")). OLEPROPERTYSET ("Italic", true); (v1.olefunction) "Font")))). OlepropertySet ("Size", 18); (v1.olefunction)

Font ")). OLEPROPERTYSET (" ColorIndex ", 6); (V1.Olefunction (" 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 line VLINE = Vshape.OLEFUNCTION ("AddLine", 90, 80, 400, 80); if (OpenDialog1 -> Execute ()) // Insert the specified bitmap vshape.olefunction ("AddPicture", OpenDialog1-> FileName, 50, 50, 80, 100);} - This program is compiled in C Builder 3. The app must first get a document before editing a document.

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

New Post(0)