When using VB programming, sometimes you need to call Microsoft Word to edit, layout, and output of text. To achieve this call, you can use the shell function, OLE Automation, embed Word objects in the package container. Through trial and comparison, the respective features of these methods have been summarized.
1 Use the shell function to call directly
Syntax: shell (pathname [, windowstyle]). PathName refers to the name of the program to be executed and any necessary parameters or command line switches, which can include directory and drive names; WindowStyle is a number of windows of the program.
Using the shell calling Word is relatively simple, the program is small, but the path where Word is specified, which is not intended to transplant, and cannot be controlled to Word, which is not conducive to data exchange between programs and Word.
2 Using OLE Automation Control Microsoft Word
2.1 How to use
(1) WORD provides an object called "Basic", to control Word in VB, first define an object variable that references "Basic" object in Word: DIM Wordobj As Object
(2) Assign the "Basic" object in Word to this object: set wordobj = createObject ("word.basic")
(3) You can use most WordBasic statements and functions to control the Word or Word document, how to use the WordBasic instructions in the Word macro is basically the same.
(4) Close Word: set wordobj = Nothing.
Note: "Basic" object does not support a way to close itself. That is, if Word is closed in OLE automation, the object is set to Nothing, and the object cannot be operated, the program is wrong.
2.2 Differences between VB Directions and WordBasic Directions
(1) Some statements and functions cannot be used, including: control structure, such as While ... Wend and if ... then ... else; declarative statement, such as DIM; custom dialog-related statement: FileExit statement; require array variable as a statement of parameters Or function.
(2) There are also some instructions to use different methods. 1 Returns the keywords that endbasic functions ending with a US dollar ($) must be enclosed in square brackets. For example, GetBookMark $ () statement in WordBasic macro: Mark $ = getBookmark $ ("address"), if you call it with VB, you must write Mark $ = Wordobj. [GE-TBOOKMARK $] ("Address"). 2 Select a command button to use "True", do not choose "false"
2.3 Description of OLE Automation
Word can automate OLE to provide an object to another application, but it cannot use OLE to automate objects in other applications. For example: VB and Excel can use OLE automation to access Word, but Word cannot use OLE automation to access them.
3 embed Word object in the bag container
In VB, to access the Word object embedded in the package container, first insert the object in the project. The approach is as follows: Insert the OLE control in the form, select "Microsoft Word Image" or "Microsoft Word Document", then press "OK".
Then use the Object property to access the document or image and use the WordBasic statement and function to work on it. Embedded objects must be activated before they can be accessed, and the OLE control can be activated using the action attribute. For example, use the following instruction to access a document in an OLE control called OLE1:
DIM Wordobj as Objectole1.Action = 7set wordobj = ole1.Object.Application. Wordbasic Other aspects, using OLE Automation. Using the Word object embedded in the package container, the Word displayed window size, the location is the same as the size defined by the OLE control, and the toolbar display position is detached from Word. This is insufficient than OLE automation.
In short, if you want to control Microsoft Word in Microsoft Visual Basic, it is best to use OLE Automation to fully control Word using WordBasic instructions, and users use to use Microsoft Word. It is very convenient.