Abstract : This paper analyzes the structure of the Word object model, especially the Document object and its usage, proposes a method for creating Word format documents in Visual Basic, and gives an application example of this method.
Keywords : VisualBasic, Word object model, document
introduction
Word document is one of the most commonly used document formats in actual work and study.
With the improvement of social informatization, some tasks that were traditionally done manually have gradually turned to computers. Computer automatic roll-up system is a typical example. In order to pursue higher efficiency, some unit users of the computer automatic examination system require that the software output the examination papers in Word format for direct printing without typesetting.
Due to the popularity of the Microsoft Office suite, documents such as Word documents, spreadsheet documents, and electronic slides are increasingly commonly used. Although documents from various programs in Office can be easily converted, this conversion comes at the expense of losing the original format of the document. For example, in the automatic examination system, the test papers stored in the access database can be generated into Word documents through the export function provided by Access. However, the generated documents are unformatted and require a lot of time to be rearranged to meet the user's test paper format. requirements.
In response to this problem, this article discusses the study of the Word object model and proposes a method of using the object model to create a Word document in a specific format that meets the user's requirements.
Word object model
1. Word object model hierarchy
Objects are the cornerstone of Visual Basic, and almost every operation in Visual Basic is related to modifying objects. Any element of Word (such as documents, tables, paragraphs, fields, bookmarks, etc.) can be represented by objects in Visual Basic.
The object represents a Word element, such as a document, paragraph, bookmark, or individual character. A collection is also an object that contains several other objects, usually of the same type. For example, a collection object can contain all the bookmark objects in the document. By using properties and methods, you can modify individual objects or entire collections of objects.
Microsoft WordVisualBasic provides a complete set of Word object models, which is an object hierarchy with application as the top-level object. Its structure is shown in Figure-1.
Objects consist of two types of members, one is properties, and the second is methods.
A property is a characteristic of an object or an aspect of the object's behavior. For example, document properties include name, content, save status, and whether revisions are enabled. To change the characteristics of an object, you modify its property values. To set the value of a property, follow the object with a period, the property name, an equal sign, and the new property value. The following example enables track changes in the "MyDoc.doc" document.
Methods are actions that an object can perform. For example, Document objects have a PRintOut method whenever the document can be printed. Methods usually have parameters that define how the action will be performed. The following example prints the first three pages of the active document.
In most cases, methods are actions and properties are properties. Using methods will cause something to happen to the object, while using properties will return information about the object, or cause some properties of the object to change.
2. Document object and Documents collection object
In Visual Basic, you can use the methods of the Document object or the Documents collection object to modify files. The Document object is the main object used in this article to create Word documents.
The structure of the Documents (Document) object set (or Document) object in the Word object model is shown in Figure-2.
The Paragraphs collection object and PageSetup object will be the protagonists below. The Paragraphs collection object is a collection of Paragraph objects in a selection, range, or document. The PageSetup object represents the page setup description. The PageSetup object contains all the page setup properties of the document (left margin, bottom margin, paper size, etc.).
On the basis of being familiar with the Word object model, we can use the Document object to perform various operations on Word documents, such as:
2.1 Create a new document
The Documents collection contains all open documents. To create a new document, use the Add method to add a Document object to the Documents collection.
One way to create a new document is to use the Add method. The Add method will return a Document object, which refers to the new document. In the following example, the Document object returned by the Add method is assigned to an object variable newDoc. Then set several properties and methods of the Document object. New documents can be easily controlled through the newDoc object variable.
2.2 Open the document
To open an existing document, use the Open method of the Documents collection. The following code opens a document named MyDocument.doc (which is located in the "MyFolder" folder).
2.3 Save an existing document
To save a document, use the Save method of the Document object. The following code saves a document named Sales.doc.
If you apply the Save method to the Documents collection, all open documents can be saved. The code below saves all open documents.
2.4 Save a new document
To save a document, use the SaveAs method of the Document object. The following code saves the active document in the current folder, named "Temp.doc".
The FileName parameter can contain only the file name or the complete path (for example, "C:/Documents/TemporaryFile.doc").
2.5 Close the document
To close a single document, use the Close method of the Document object. The following code closes and saves the document named Sales.doc.
All documents can be closed using the Close method of the Documents collection. The following code closes all documents without saving changes.
Create a Word document in Visual Basic
1. Create a Document object in Visual Basic
All work starts with the Document object. First, create an instance of the Document object in Visual Basic, and then you can perform various controls on the instance:
Once the Document object is created, you can set the default format of the document by setting the font, line spacing and other properties of the Content sub-object:
This results in an empty Word document.
2. Add text to a Word document
Next, add text to the empty document. You will use the Paragraphs collection object to do this. The InsertAfter method inserts text after the Selection or Range object; the InsertBefore method inserts text before the Selection or Range object. The following code adds and formats a paragraph to the end of the document:
Paragraphs(index) can be used to return a Paragraph object, where index is the index number; the count attribute value indicates the number of Paragraph objects in the main text part of the document. NewDoc.Paragraphs.count is exactly the index number of the text paragraph currently being added.
If you need to insert a table, you can use the Table object. The Add method can add a new table within the specified range. The following example adds a 3x4 table at the beginning of the active document.
Tables can be obtained using tab characters. The following code inserts a 1x4 table at the end of the document.
The created table is shown in Figure-3:
3. Page settings
After all paragraphs have been added to the document, you can use the PageSetup object to set up the page. This needs to be done by setting various properties of the PageSetup object.
3.1 Set columns and column spacing:
3.2 Set page margins:
3.3 Set paper size:
Other setting items will not be described in detail.
4. Output documents
For the created document, we can choose to save it as a disk file or print it out directly:
Application examples
The above method is applied in the computer automatic examination system developed by the author for the Organization Department of a municipal party committee. The system process is shown in Figure 4.
The test papers generated by this system are required to be directly used in the examination to assess and select grassroots cadres. First, the question maker inputs the test paper parameters, that is, the rules for forming papers, and then the software automatically composes the papers and outputs the papers in Word format. The software system developed by the author using the above method fully meets user needs. The system has now been put into operation, which has greatly improved work efficiency and won praise from users.
Conclusion
The method of creating Word documents in Visual Basic proposed in this article is based on the object model and is highly practical. It can also be used in other development platforms that support ActiveX. ->