Hello everyone! In the last post we left off with a small example of how to automate Microsoft Office applications from our custom solutions. This week we are going to discuss about what does Microsoft Office object model means, and will discuss about various classes/interfaces available in the Office applications.
As I had mentioned earlier, Microsoft Office applications (from now on, wherever I mentions this term, I am referring to Microsoft Word, Excel, and PowerPoint) are also COM (Component Object Model) servers. This enables them to provide a way for developers to use their features in other programs.
As a developer, all you need to do is call a function to launch the server, and once it is up and running, you can ask for the various interfaces provided by the server and utilize its functionality into your application.
The task of launching an Office application from your program can be performed in different ways depending on whether you are linking to the COM server’s type library statically, or you want to launch it dynamically. While linking statically, all you need to do is call the constructor of the main class of the COM Server and then you can access the other Interfaces by using properties/methods of the main class object. Linking dynamically involves calling CoCreateInstance API specifying either the CSLID or ProgID of the application to launch. In the example code of my previous post, we have seen the static linking technique.
Each of the Office applications provide their main class by the name Application, and rest of the Interfaces/Classes that we can use are exposed as Properties of the Application class. It represents a running instance of the Sever and provides methods to manipulate its state like, activating it, setting its visibility etc.
After the Application class, each Office application provides a class that represents the document it works on. In case of Word it is Document class, for Excel it is called Workbook and in case of PowerPoint it is named as Presentation. The Application class maintains the list of all currently opened documents and provides a property that represents this collection. To access a particular document, we need to first get the collection and then find our required document object.
In case of Excel and PowerPoint, each working document contains another level of isolation for its contents. Excel provides separate Worksheets in a workbook and a PowerPoint presentation contains various Slides. You might say that even Word documents contain various Pages, and you are very right. However, Word doesn’t provide page as another isolation of its content. All of the content of a document is represented by the Document class only.
Ok, I think that is enough of the theory; let’s see how this architecture of Office applications can be depicted:
As shown above, Range class is common to Word and Excel, and it represents the portion of the content that can be manipulated with these applications. In case of Word, it could represent a paragraph, a line of text, or a single word. In case of Excel, it represents a collection of Cells, or a particular cell. In PowerPoint, the content of a slide is represented by a collection of Shapes, i.e., each item of a slide in PowerPoint is a shape.
A point to be noted here is that even though I have mentioned Range/Shape as THE object to manipulate content in Office application, it not the only one. There are other objects, too, that can be used to play around with the content of these applications. Here is a small list of such objects:
- Selection – type of Range object
- Shapes – represents drawings, embedded images or objects, etc.
- InlineShapes – shapes that are displayed in-line with text
- Rows – Range object representing a row of data in Excel
- Columns – Range object representing a column of data in Excel
- Cells – another Range object, for a single/multiple cell(s)
- Shapes – represents drawings, embedded images or objects etc.
- Selection – Represents any selected item on the sheet, could be a Cell, a Range of cells, or a Shape etc.
- Selection – represents a currently selected object on the slide
- SlideRange – object to manipulate single or multiple slides
- TextRange – object to manipulate a selected piece of text
- ShapeRange – object representing a collection of shapes
In Part-1 of this post, we saw code snippets in VC++ and C# to insert a piece of text in a Word document. If you were to implement similar task for an Excel workbook or a PowerPoint Presentation, you can very easily implement it by knowing what type of object you should work with. For instance, you can use Range or Cell object in Excel, and you can use Shape object in case of PowerPoint.
With this, I am going to end this post. I am sure this post has broadened your view of how to work with Office applications. You can also refer to Microsoft Office SDK Documentation and Developer References for more details and list of other classes/objects available to you.
In case you have any queries, please feel free to drop in your comments; I will be more than happy to respond.