When you are programming with VB, you sometimes need to invoke Microsoft Word to edit, format, and output text. To implement this invocation, you can use Shell functions, OLE Automation, embedding Word objects in the containment, and so on. After trial and comparison, the characteristics of these methods are summed up.
1 using the Shell function to call directly
Syntax: Shell (Pathname[,windowstyle]). Pathname refers to the name of the program to be executed and any required arguments or command-line switches, which can include directories and drive names; WindowStyle is the number of window styles that execute the program.
Using the shell to invoke word is simpler and less programmatic, but you must explicitly specify the path where Word is located, which is not conducive to porting, and you cannot control word, which is not conducive to data exchange between programs and word.
2 using OLE Automation to control Microsoft Word
2.1 How to use
(1) Word provides an object called "Basic" for OLE automation, and to control word in VB, you first define an object variable that references the "basic" object in Word: Dim Wordobj As Object
(2) Assign the "Basic" object in Word to the object: Set wordobj=createobject ("Word.Basic")
(3) You can use most WordBasic statements and functions to control word or Word documents, using methods and methods that use WordBasic directives in Word macros are basically the same.
(4) Close Word:set wordobj =nothing.
Note: the "Basic" object does not support a method that closes itself. Where closes word in OLE Automation, the object is set to nothing, and the object cannot be manipulated, and the program is faulted.
The difference between 2.2 vb instruction and WordBasic instruction
(1) Some statements and functions can not be used, including: control structure, such as while ... Wend and if ... Then ... Else; Declare statements such as Dim; Customize dialog-related statements: FileExit statements, statements or functions that require an array variable as a parameter.
(2) There are also some instructions to use different methods. ① the keyword of a WordBasic function that returns a string ending with a dollar sign ($) must be enclosed in square brackets. For example, in the WordBasic macro getbookmark$ () statement: mark$=getbookmark$ ("Address"), if called by VB, you must write Mark$=wordobj. [ge-tbookmark$] ("Address"). ② Select a command button with "True" and do not select "False"
2.3 Description of OLE Automation
Word can provide an object to another application for OLE Automation, but it cannot use OLE Automation to access objects in other applications. For example, VB and Excel can use OLE Automation to access word, but Word cannot access them using OLE Automation.
3 Embedding Word objects in the containment device
In VB, to access the Word object embedded in the containment, you first insert the object in the project. As follows: Insert an OLE control on a form, select MicrosoftWord picture or Microsoft Word document, and then press OK.
You then use the object property to access the document or picture, and use WordBasic statements and functions to work with it. Embedded objects must be activated before they can be accessed, and you can use the Action property to activate the OLE control. For example, use the following instruction to access a document embedded in an OLE control called OLE1:
Dim Wordobj as Object
OLE1.Action =7
Set Wordobj =OLE1.Object.Application. WordBasic
In other ways, use the method with OLE Automation. With Word objects embedded in the containment, Word displays the same size and position as the OLE control, and the toolbar display location is detached from Word. This is a disadvantage compared to OLE automation.
In summary, to control Microsoft Word in Microsoft Visual Basic, it is best to use OLE Automation, to have full control of Word by using the WordBasic directive, and to use Microsoft Word as a user, Very convenient.