Sunday, 19 August 2012

Creating PowerPoint 2010 AddIn in c#

Creating PowerPoint Add-ins quite simple because of the nice documentation at MSDN and an extensive set of objects and methods which lets control each and every aspect of PowerPoint Interface.

The Add-in I created allows one to control a PowerPoint show with an android device.
The project thus consists of creating both an android app and the Add-in.
Since the two are created to work on different platforms thus an interface must be decided  that allows the two software to talk to each other. The interface I decided upon was a UDP connection over wifi in which the android app would send the 'number'/'id' of the button pressed and the Add-in would continuously check the sent number with a switch statement inside of an infinite while loop.

IMP: One thing to be careful of is that the addIn must not contain any long running code (such as those in socket programming) or sleep statements as the PowerPoint slideshow becomes non-responsive (addIn(s) are called in a blocking manner). So if such a code is necessary then a new thread must be created for it.

Creating Add-in consists of two main parts:

  1. Creating event handlers i.e. telling PowerPoint that this is the code you have to execute when certain things happen for eg. if you want to display a message box when 'slide-show' begins then you first create an event handler (viz just a function) and add the handler to the list of handlers associated with that event. so for our message box  example the code would be:
    • Application.SlideShowBegin += OnSlideShowBegin; //add OnSlideShowBegin to the list of functions that fire when slideshow begins.
    • void OnSlideShowBegin(SlideShowWindow Wn){MessageBox.Show('Slide Show Started');} //defining the code to be executed on function call
  2. Accessing the object model exposed by PowerPoint. What this means is that to interact with PowerPoint we need to access the various functions that are provided by the PowerPoint environment. For this there are various objects which are well documented on MSDN (here).
We start off by creating a new project in VC2010 and selecting PowerPoint 2010 Add-in template .
VS will automatically create the basic code structure containing a few event handlers. The Function
'private void ThisAddIn_Startup(object sender, System.EventArgs e)' is called on PowerPoint start-up. Since our Add-in is required to work only after the slide-show is started thus in the start-up function we add a function to the list of the functions executed when the slide-show starts. The 
code for it is:
      Application.SlideShowBegin += OnSlideShowBegin; //add a user defined                              function 'OnSlideShowBegin' to the list

In the definition of 'OnSlideShowBegin' we just create a new thread which will handle the socket connection. The 'OnSlideShowBegin' as an argument receives a 'SlideShowWindow' which contains the required methods to interact with the show. The functions required to change the slides, simulate mouse clicks , etc . can also be accessed by using the object. One good thing about the object is that it also gives access to the window's handle thus any other operation not directly supported by the API can be performed.

The android app is pretty self explanatory as there are only two parts to it:
  • connecting to Add-in's socket, and
  • sending the 'button number' on press of a button

The project was created by me in a duration of few hours and thus has quite a few Bugs. some of them are:
  • The App cannot connect to the Add-in if the slide show is started a second time, PowerPoint must be restarted. -BUG FIXED
  • The App and the Add-in have crashed quite a few times.
The safest way to make the Add-in work is by starting a new PowerPoint Application, opening the presentation and starting the slide show. Once the slide show has started the ip address and port of the computer running the presentation must be entered in the android app (separated by colon, for eg. As of now the port no 11000 is hard coded in the application and if it is to be changed , the application must be compiled again.

The code for the android app and the Add-in can be obtained from github at: 
the addin folder contains the c# code for the Addin and the Android App folder contains the code and other resources of the android app.

Additional Resources: