VSTO Tour Series (iv): Create a Word solution

Source: Internet
Author: User

Original: VSTO Tour series (iv): Creating a Word Solution

Summary of the topic

    • Introduction
    • Word object Model
    • Create a Word add-in
    • Summary

First, Introduction

In the previous topic, we'll show you how to customize our Excel interface, but in this topic I'll show you how to create a Word project with VSTO, similar to Word's VSTO development and Excel development, and you can also customize the interface for Word. They differ mainly in the object model, as long as you are familiar with Word's object model, it is easy to manipulate word. The following is the first introduction to the object model of Word.

Second, the Word object model

Before you create a word solution and create an Excel solution, you need to understand their object model, because you can better work with Word and Excel documents by understanding the relationships between the objects in them, and you'll first give a Word object Model hierarchy chart:

Look at the above friends are certain of the relationship between the object is somewhat understood, but for each object in the end what the meaning or not understand, the following specific for everyone to introduce the meaning of each object represented.

The Application Object represents the Word application, and we open a Word document that opens a Word application, and it's important to note that-- no matter how many Word documents you open, it's managed in a word process . It is the parent of all objects (as you can see, each object is its child node).

In the application add-in project (which is covered in the Excel solution, there are application add-in projects and document-level projects for word), we can get the Application object in the following way: Globals.ThisAddIn.Application

In a document-level project, we can get the application object through the Application property of the ThisDocument class, i.e. the code is: word.application app = Globals.ThisDocument.Application;

The document object represents a Word document, even if you open an empty document, there is a document object, we need to get the object in advance before we edit, modify, delete, and add the contents of the Word document. The focus of the document we call the active document, we can get the current active document through the ActiveDocument property of the Application object, the code is:

using Word = Microsoft.Office.Interop.Word; // Obtaining Document Objects            in application-level projects Word.Document doc= Globals.ThisAddIn.Application.ActiveDocument; // Getting document Objects            in document-level projects Word.Application app = Globals.ThisDocument.Application;             = App. ActiveDocument;

The Selection Object represents the currently selected region. When we do certain things in Word, for example, changing the font size of the text, we need to select the text and then set the font size of the selected text so that the selected text represents a Selection object, which you need to be aware of-- Selection objects are always present in the document, because some friends ask, if we don't have any text selected, are there selection objects? The answer is yes, when we do not select any text, the insertion point (that is, the action text will have a symbol "|", the symbol represents an insertion point) is a Selection object, and the selected content can contain multiple discontinuous blocks of text .

The Range object represents a contiguous range, determined by a start character position and an ending character position. We define multiple Range objects in the document, and when we see the Range object, friends think of the selection object, they all represent the region, what is the difference between them? For their differences-theRange object is invisible in the document and is contiguous, and the Selection object is visible in the document and can contain multiple discontinuous blocks of text . In layman's terms, for example, we want to change the font size for some text, and there are two ways to do this: the first is to use the Selection object to select the text and then change its style (the text that is selected at this point can be seen in the Word document, We can clearly understand that the text needs to change the font, and the second is to use a Range object to specify the position of the start character (that is, a literal) and the position of the ending character, so that between the start and end characters is a Range object, The Range object is then set to its font style, but this whole process we don't see the text being selected, so before we apply the style, we don't know that the text will change the font, but instead use the Selection object, even before applying the font style, We can see with the naked eye that the text will change the font.

Bookmark Objects are bookmarks, in real life, we use bookmarks to make tags . There is also a bookmark in the Word document, we can make a mark on word so that you can know where you last read, and the bookmark is such an object. Bookmarks can be hidden or become visible, and we can set the ShowBookmarks property of the View object to true or false.

To help understand these objects further, the following diagram shows the corresponding relationship of each object in a Word document:

Iii. Creating a Word add-in

The main objects in the Word object are described above, and the main objects are used to manipulate the word documents, so how do we better grasp the use of these objects? The method is to write more programs to practice. The following is a common requirement-converting a Word document to a PDF or XPS file. here for how to create an add-on project is not described, the specific steps and create an Excel solution is the same, just select the template is not the same, Excel is selected in the Excel 2010 add-in template, Word is natural selection is word 2010 add-in templates. The following is a detailed description of the process for implementing this requirement:

    1. After you create the word 2010 add-in, right-click the item--Add a new item, select the Ribbon (Visual designer), and in the Name section, enter: "ExportPDFRibbon.cs";
    2. Click the Toolbox, drag the menu control into theOffice Ribbon Control , and then insert a two button button in the menu control, named PDF and XPS
    3. Design Ribbontab, because the ribbon designed in the previous topic is a separate tab, so in this project I do not want to create a separate ribbon, want to put the control under the Ribbon under the Home tab, in order to achieve this goal, you must set several properties in the For specific properties, see:

4. Insert the following implementation code in the background code of the ExportPDFRibbon.cs file (which you can press F7 to view the acquired code):

 Public Partial classExportpdfribbon {Private voidExportpdfribbon_load (Objectsender, Ribbonuieventargs e) {            //Register click-time for PDF and XPS buttonsBtnribbonpdf.click + =NewRibboncontroleventhandler (exportdocument); Btnribbonxps.click+=NewRibboncontroleventhandler (exportdocument); }        //Export File Method        Private voidExportdocument (Objectsender, Ribboncontroleventargs e) {                      Switch(e.control.id) {//determine the button ID of the click                 Case "btnribbonpdf":                    //Open the Save File window                    using(SaveFileDialog SaveFileDialog =NewSaveFileDialog ()) {                        //set the related properties of the Save File windowSavefiledialog.filter ="All File (*. *) |*.*"; Savefiledialog.defaultext=". pdf"; Savefiledialog.restoredirectory=true; //in the Save File window, click the Save button                        if(Savefiledialog.showdialog () = =DialogResult.OK) {//Export to PDF formatGlobals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat ( Savefiledialog.filename, Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormat                        PDF); }                    }                     Break;  Case "Btnribbonxps":                    using(SaveFileDialog saveFileDialog2 =NewSaveFileDialog ()) {Savefiledialog2.filter="All File (*. *) |*.*"; Savefiledialog2.defaultext=". XPS"; Savefiledialog2.restoredirectory=true; if(Savefiledialog2.showdialog () = =DialogResult.OK) {//Export to XPS formatGlobals.ThisAddIn.Application.ActiveDocument.ExportAsFixedFormat ( Savefiledialog2.filename, Microsoft.Office.Interop.Word.WdExportFormat.wdExportForma                        TXPS); }                    }                     Break; default:                    return; }        }    }

You can do this with the above steps: Save the Word document as a PDF or XPS file, and run the result as follows:

After clicking the PDF button, the Save As window will pop up, the name you want to save is entered in the pop-up window, and when you click the Save button, you can see the saved file name in the saved location.

Iv. Summary

Here, the content of this topic is completed, this topic mainly introduces the Word object model, through the object model of the use of the object to the Word document operation, about the Word document more action content I will be in the implementation of the Office Automation topic to introduce to you, Finally, a simple requirement-converting Word files to PDF or XPS files is implemented. I hope that through this topic you can use VSTO technology to do word operations, and then in the next topic will introduce you to the Outlook related content.

VSTO Tour Series (iv): Create a Word solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.