Requirements: Use VSTO to develop a simple plug-in for Office, such as getting the Word Chinese character length plug-in.
Word object model
Word offers hundreds of objects to interact with. These objects are organized in a hierarchy that strictly follows the user interface. The Application object is at the top of the hierarchy. This object represents the current instance of Word. The Application object contains the Document, Selection, Bookmark, and Range objects. Each of these objects has a number of methods and properties that can be used to manipulate and interact with the object.
The following image shows a view of these objects in the Word object model hierarchy.
Documentation:The hyperlink login is visible.
Application object
The Application object represents the Word application and is the parent of all other objects. Its members are usually applied to Word as a whole. You can use its properties and methods to control the Word environment.
In a VSTO add-in project, you can access the ThisAddIn object by using the Application fields of the Application class.
In a document-level project, the ThisDocument object can be accessed by using the Application property of the Application class.
Document object
The Document object is the center of Word programming. It represents a document and all its contents. When you open a document or create a new document, a new Document object is created and added to the Application collection of the Documents object. A document with a focus is known as an active document. It is represented by the Application property of the ActiveDocument object.
When you create a document-level project, you can access the ThisDocument member by using the Document class generated in the project. The ThisDocument object can be accessed by using the Me or this keyword for code in the Document class, or by using Globals.ThisDocument for code outside of the ThisDocument class.
Selection object
The Selection object represents the currently selected region. When performing an action in the Word user interface, such as bolding text, you can select or highlight the text and then apply the formatting. The Selection object is always present in the document. If nothing is checked, it indicates an insertion point. In addition, the selected content can contain multiple non-adjacent blocks of text.
Range object
The Range object represents adjacent areas in the document and is defined by the start and end character positions. It's not limited to a single Range object. You can define multiple Range objects in the same document. Range objects have the following characteristics:
- It can contain only individual insertion points, or it can contain a range of text or the entire document.
- It includes non-printed characters like spaces, tabs, and paragraph markers.
- It can be the area represented by the currently selected content or the area that is different from this content.
- It is not visible in the document, unlike the selected content, which is always visible.
- It is not saved with the documentation and only exists when the code is running.
- When text is inserted at the end of a range, Word automatically expands the range to include the inserted text.
Content Control Objects (Content Control Objects)
ContentControl provides a way to control the input and rendering of text and other types of content within a Word document. ContentControl can display many different types of UI that are optimized for use in Word documents, such as multi-message text controls, date pickers, or combo boxes. You can also use ContentControl to prevent users from editing certain sections of a document or template.
Visual Studio extends the ContentControl object to several different host controls. While the ContentControl object displays all the different types of UI available for content controls, Visual Studio provides a different type for each content control. For example, you can use RichTextContentControl to create a multi-message text control, or you can use DatePickerContentControl to create a date picker. These host controls behave similarly to native ContentControls, but they also have additional event and data binding capabilities.
Bookmark object
The Bookmark object represents adjacent areas in a document with both start and end positions. You can use bookmarks to mark a location in the document or as a container for the Chinese version of the document. A Bookmark object can contain insertion points or be as large as the entire document. Bookmark has the following characteristics that distinguish it from Range objects:
- You can name bookmarks when designing.
- The Bookmark object is saved with the document, so it is not deleted when the code stops running or the document is closed.
- Access the View property by setting the View property to false or true.
- Visual Studio extends the Bookmark object by providing a Bookmark host control. Bookmark host controls behave similarly to native Bookmark, but they also have additional event and data binding capabilities. You can bind data to a bookmark control on a document in the same way you bind data to a text box control on a Windows Form.
Create a Word VSTO add-in
After understanding the basic concepts, open VS 2022 to create a new Word VSTO add-in, as shown in the figure below:
Create a visual ribbon as shown in the following figure:
Double-click Ribbon1.cs to open, remove the default tab tab, and drag a new tab in, as shown below:
Drag a group and a button control into the new tab tab, as shown below:
Double-click the newly created button button to create a click event with the following code:
(End)
|