Manipulate OLE server applications in VB

zhaozj2021-02-11  205

Manipulating OLE Server Application OLE Automation in VB is a standard for communicating between different applications. OLE automation work is: Communication Directive (OLE server) application provides an OLE automation object type that can be called by the Active (OLE client) application, the OLE client implements the OLE by reference to these objects. Call, then manipulate the communication between the two by setting the object's properties and methods of using the object. Visual Basic is a fully supported an OLE automation application development tool. Using VB, you can also prepare applications as an OLE server, or applications as an OLE client. MS Word 6.0 is an application software that does not fully support OLE automation, which can only be called as OLE servers for other applications. This article will use a VB application to manipulate the MS Word 6.0 by OLE Automation, which describes how to manipulate the OLE server application in VB. OLE server calls in VB code as follows: control type properties Property Value Data Control DataBaseNameName C: /VB/BIBLIO.MDBData1 RecordSource Authors DBGrid ControlCommandButton Control DataSource Data1 Name cmdReport Caption report CommandButton Control Name cmdExit Caption exit ⑴ declare an object variable . Such as: DIM MS-Word As Object 2 Depending on the object type provided by the OLE server, create an object using the CreateObject function and give the object to the object variable by the SET statement. The syntax of the CreateObject function is as follows: createObject (Class) Function Parameter Class's format is: appname.ObjectTyPEAPNAME is the name of the application of an object, such as WordObjectType is a class that wants to create, such as Basicms Word 6.0 is OLE client application The program provides an object type "Basic", and VB can use the object type to implement calls to Word. In particular statement is as follows: Set MS-WORD = CreateObject ( "Word.Basic") ⑶ by using the object properties and a method of setting the object, manipulation of the OLE server achieved. Object Type "Basic" uses most of the statements and functions of Word Macro Wordbasic as its method. That is, once a "Basic" object is created in the VB, most of the WordBasic statements or functions can be used through this object, so that Word or Word documents can be perfectly perfectly perfect. For example, the following statement uses WordBasic's FileNewDefault statement to create a Word New Document: MS-Word.FileNewDefault ⑷ After calling, use keyword nothing to release the resources occupied by the variable. Such as: SET MS-WORD = Nothing instance This example will write a simple database application using VB. The program removes data from the database, and then inputs these data to Word via OLE Automation, and arranges as a table according to Word's typography format. The database used in the example is the Bilio.mdb database independent of the VB, and the data retrieval result is taken from the Authors data sheet. This example is slightly modified, which can be used as a report generating function module for the database application. First, create a form called Form1 and set its CAPTION property to "OLE Automation Demo".

Then add four controls in this form and set the relevant properties, as follows: After completing the above work, pressing the screen layout of F5 to run as shown below. Then you need to write VB code to implement connection with Word. The code list of this example is as follows (Note: The slope portion of the code is the statement of the WordBasic macro language, please refer to the WordBasic macro language information): (1) Part of the General Declarations in Form1 declares form-level global variables and constants . DIM MS-WORD AS Object 'declares that an object variable const marxcols = 5' declares a constant for indicating the number of fields in the current data record set. In this example, the number of fields is 5 2 () Creating a subroutine insertTableintoms-word (), which uses the WordBasic Macro Solution to create a table in Word 6.0, and insert the data in the current data record set into the table unit.

Sub InsertTableIntoMS-Word () Dim i As Integer, j As Integer, Col As Integer, Row As IntegerDim CellContent As StringMe.HideCol = MaxColsRow = Data1.Recordset.RecordCountMS-WORD.FileNewDefaultMS-WORD.MsgBox "is to create a report, please wait designate ... "," ", -1MS-WORD.LeftParaMS-WORD.ScreenUpdating 0MS-WORD.TableInsertTable, Col, Row,,, 16, 167MS-WORD.StartOfDocumentData1.Recordset.MoveFirstFor i = 1 To MaxColsCellContent $ = Data1.Recordset .Fields (i - 1) .namems-word.insert CellContent $ ms-word.nextCellnext IDOFOR i = 1 to maxcolsif isnull (data1.recordset.fields (i - 1) .value) ThencellContent $ = "" ElsecellContent $ = data1 .Recordset.Fields (i - 1) .ValueEnd IfMS-WORD.Insert CellContent $ MS-WORD.NextCellNext iData1.Recordset.MoveNextLoop Until Data1.Recordset.EOF = TrueMS-WORD.TableDeleteRowMS-WORD.StartOfDocumentMS-WORD.TableSelectRowMS-WORD .TableHeadings 1MS-WORD.CenterParaMS-WORD.StartOfDocumentMS-WORD.ScreenRefreshMS-WORD.ScreenUpdating 1MS-WORD.MsgBox "end", "", -1Me.ShowEnd Sub⑶ command button control cmdReport mouse click event code Private Sub cmdRep ort-Click () Dim ResponseScreen.MousePointer = 11Set MS-WORD = CreateObject ( "Word.Basic") MS-WORD.AppActivate "Microsoft Word", 1Call InsertTableIntoMS-WordScreen.MousePointer = 0End Sub⑷ command button control cmdExit mouse click Event code private subdexit-click () set ms-word = Nothingunload Mend Sub Address 1 Preface before this program is running, Word 6.0 must have been successfully installed, otherwise a VB capture error will be generated. Word 6.0's table column cannot exceed 31 columns, otherwise the system will generate a VB capture error. The reader can add an error handling code to handle these errors in the code. 2 If the program is running, the Word has not yet runned, and OLE automation will try to start it. Therefore, there is no need to include a separate instruction to start the Word. If Word is started by the VB application, Word will be automatically turned off at the end of the program; otherwise, Word will continue to run. 3 Most of the WordBasic statements used by this example are related to the insertion point of the Word document. If the person moves in the process of running in this program, it is possible to cause the form of chaos and errors.

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

New Post(0)