How to control Word in VB

zhaozj2021-02-17  46

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 syntax directly: shell (pathname "). PathName refers to the name of the program to be executed and any necessary parameter or command line switch, which can include the directory and drive name; WindowStyle is the window style of the execution program Number. 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 Usage Method (1) Word provides an object called "Basic", to control Word in VB, first define an object variable of "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 function to control Word or Word documents, how to use And the method of using the WordBasic instruction 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 Vibration of VB Directions and WordBasic Directive (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 variables as statements or functions of parameters. (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 for OLE Automation Word to automate the OLE to provide objects, 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 a Word object in the package 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.

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

New Post(0)