I am really excited to write this Article, on Microsoft Office Object Model. To extend the functionalities of Microsoft Office Applications (like Excel, Word, etc.,) Microsoft has provided Office Object Model. Let me give a brief introduction to Microsoft Office Development.
Microsoft has provided Object Models to allow to extend the functionalities of Microsoft Applications. These extensions can be done through Add-ins. The Developer / User who is going to develop the add-ins should follow Microsoft Object Model which contains objects that allow interaction to extend the Application’s functionality.
The main Object in Microsoft Office Object Model is the Application Object. This gives reference to the Application instance which is currently running. For example: If you are running, Microsoft Outlook Application; the Application object will refer to this Outlook Application’s instance. So, that allows you to access the Microsoft Outlook Application.
I will start this series of articles, by developing a simple add-in using C#. The add-in will display a message when the add-in is going to start & another message when the add-in is going to stop.
Assuming you have already installed Visual Studio Tools for Office Development on your System.
Step 1. Create a new project from the Template “Office 2010”. Select “Outlook 2010 Add-in” to create the new project.
Step 2. Visual Studio will create most of the code for us. By clicking the “ThisAddIn.cs” file, you can see the code the Visual Studio has created.
Step 3. There are two methods, “ThisAddIn_Startup” and “ThisAddIn_Shutdown” added in the code. “ThisAddIn_Startup” will be called, when the Startup even is generated; when the AddIn is loaded. “ThisAddIn_Shutdown” will be called, when the Shutdown event is raised; when the AddIn is unloaded.
Step 4. We will use these two methods, to display a message “Hello!” when the AddIn is loading/starting, and display a message “Goodbye!” when the AddIn is released / shutdown / unloaded.
Step 5. Put the below statement inside the “ThisAddIn_Startup” method.
System.Windows.Forms.MessageBox.Show(“Hello!”);
That means, the statements added inside “ThisAddIn_Startup” will be executed when the AddIn is loaded.
Step 6. Place below statement inside “ThisAddIn_Shutdown” method.
System.Windows.Forms.MessageBox.Show(“Good bye!”);
The above statement will execute when the AddIn is released from the memory.
Step 7. Our complete code looks like below. Note that, most of the code has already been added by VisualStudio.
Step 8. Lets’ compile and RUN the program. Observe that, our Program will open Microsoft Outlook Application and display the message “Hello!” on the screen. That means, our Program is working fine.
Behind the scene, VisualStudio will do lot of things to us to simplify the process. One of such is Registering the Add-In. This tells to Outlook Application to use our Add-In.
Step 9. Now we will expect to see a “Good bye!” message when we close the Outlook Application. Close Outlook Application. And observe that, “Goodbye!” message does not appear on the screen. Why? Office Applications will NOT ALWAYS call the “ThisAddIn_Shutdown” method when the Application is going to shut down, due to performance reasons.
But we can verify whether our statements inside “ThisAddIn_Shutdown” are working fine, with the below steps.
Step 10. Once Outlook Application is opened, open the “COM Add-Ins” dialog. This will allow enabling/disabling the Add-Ins for Outlook. You can check this article “How to manage Add-Ins in Microsoft Office?” on managing Add-Ins.
Step 11. Locate our Add-In, in the “COM Add-Ins” dialog and un-check the checkmark, marked for the Add-In. This will shutdown / release the Add-In. Click on the OK button to release the Add-In. Observe that, “Goodbye!” message appears on the screen.
You can do the testing by checking/unchecking the check box, to start/stop the Add-In and observer that, “Hello!” and “Good bye!” messages will appear whenever the Add-In is loading & unloading respectively.
I hope this article helps you. Please give your feedback through comments.
(Raju)
2 thoughts on “Outlook Add-In : Creating a Simple Add-In Using C#”