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:

 Office Object Model Architecture

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:

  • Word
    • Selection – type of Range object
    • Shapes – represents drawings, embedded images or objects, etc.
    • InlineShapes – shapes that are displayed in-line with text
  • Excel
    • 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.
  • PowerPoint
    • 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.

3 Responses to “Demystifying Microsoft Office Object Model – Part 2”

  1. Mukta says:

    Hello Manvir,
    First of all thanks for compiling lots of good and helpful information here. This has given a very clear and concise understanding of Interop classes.

    Hope you can help me in what I am trying to do :)

    I am using Office 2010 and VS 2008. The problem I am facing is – I have few read-only documents accessible through my app.
    i) I want to disable auto-Save feature for only these documents.
    ii) I want to disable “Save As” button for these documents as I have different save operations for these kind of documents. This also means that I cannot suppress beforeSave operation. :(

    Do you think that with the visibility of my interop.word object, I can accomplish this?
    Any pointers will be helpful.
    Thanks a lot.

    Regards.

  2. Manvir says:

    Hi Mukta,

    It’s good to know that my posts were helpful! :)

    Regarding your questions, I will have to check about disabling the the auto-Save feature. However, to disable the Save-As option, you can repurpose the Office Ribbon command, and execute custom logic or just cancel the Save-As operation by setting the one of the arguments of command’s handler to false.

    You can very well accomplish this using from within your VSTO solution, and using Word interops.

    Please let me know if I misunderstood your concerns, or if you need any information related to this.

    HTH,
    Manvir Singh

  3. Ricardo Sousa says:

    Hello Manvir,

    Great post.

    Have you ever tried to create a chart without any window?
    I’ll be more clear:
    I’m trying to create a PowerPoint presentation from scratch but without any window.
    All works fine except Charts, when I add a chart into a slide excel pops-up.

    Any idea?

    Thanks

Leave a Reply