For one reason or another, the other day I started to look in to automating Office applications with the .net framework. Its a bit of a mess and quite confusing. Depending on whether you want to create a new function in Excel that you can enter in a cell, a control to add to a ribbon, or fully automate the creation of a spreadsheet, document or presentation there are different approaches and methods. Its never simple!
I hope to spend some time working through the different open source options for libraries that make working with Office a lot simpler (ExcelDNA is a good one for Excel, as an example). To start with, I’m going to create a console application that will show some of the basic setup steps for creating a new presentation.
Getting Started: Project Setup
First thing’s first. Lets create a new Console Application project in Visual Studio. Make sure that the project targets the 4.5 .Net Framework.
We have a fair bit of tidying up to do now. First we need to add some references. These can all be found under Assemblies, Extensions in the References Dialog box.
You will need to add the following references
First Steps: Initial Code
To start with let’s sort out our Using statements.
I have assigned some shortcut words to the path, so as you can see PowerPoint. would be the same as specifying Microsoft.Office.Interop.PowerPoint. Smart hey? Next, let’s sort out handling an input parameter for our application. I’m going to keep this really simple. We will specify a string as a parameter when we execute the program. Assuming the user specifies a string, the string will get passed to a method that will use the string to place text on to a slide in PowerPoint. The initial code is below.
You will see that I have create the base layout for the CreatePresentation method. This method will handle all the PowerPoint interoperability. It takes the parameter specified by the user. Next, lets start getting our code in to work with PowerPoint.
The two variables ofalse and otrue are used later on in the code. Limited details on the MsoTriState enumeration can be found on MSDN. I have hard coded the path to a PowerPoint file. I know, it’s bad practice, but I am keeping this tutorial simple, so you can feel free to have this specified as an additional parameter from the command line if you want to. If the file doesn’t exist, the program will create it.
Next, I create a handle to the PowerPoint application (PowerPoint.Application). To be clear, PowerPoint isn’t already running. This will create an instance to be used by the code. Finally, I have create a handle to a new Presentation object. The Presentation object gives you access to a PowerPoint presentation (existing or new). This will be passed to the PowerPoint application handle which will look after saving the presentation. I’ll finish off setting up the presentation object with the next piece of code. This will allow us to demonstrate editing an existing presentation if it exists, or creating a new one if it doesn’t.
As you can see in the above code, if the file specified doesn’t exist, then a new presentation is created and added to the PowerPoint application Presentations object. However, if the file already exists, we have to open it.
This line warrants an explanation. The .Open method takes a number of parameters as can be seen above. The parameters are not optional.
Parameter1 – The filename (including path) of the file to open.
Parameter2 – Whether or not to open the file as readonly.
Parameter3 – Specifies whether the presentation has a title or not.
Parameter4 – Specifies if the presentation window (presentation in PowerPoint) should be visible on the screen.
Making Progress: Adding a Slide
Next we can start to implement the code to create a new slide and add it to our presentation. The presentation object has a member called Slides. Slides returns a collection of slide objects; ultimately Slides contains a collection of all the ‘PowerPoint Slides’ in the presentation. To add a slide you have to specify an index (a location) of where to add the slide in the slides collection. The starting index is 1, not 0. So we always have to set our first slide to have an index of 1. What if we have existing slides though? If we want to add a new slide to the end of the slides collection (the new slide will become the last slide in the presentation) we need to make sure it has the highest index value.
There is a very simple way to accomplish this.
Line 45 – Get a count of all the slides in the presentation.
Line 46 – Add 1 to the total number of slides. Store this result as the index for our new slide
What does this mean?
- If this is the first slide in the presentation the index will be 1
- If this is not the first slide in the presentation, its index will be (total number of existing slides + 1)
Line 48 is where the new slide is added to the slides collection. The Slides collection has an Add method that makes it easy for us add new slides. It takes two parameters
Parameter1 – The index in the collection for the new slide.
Parameter2 – The layout type for the new slide.
For parameter 2, there are multiple types of layouts that you can supply. These match up to the slide layout types you see in PowerPoint when you create a new slide object. Additionally, MSDN details each one.
Creating Slide Content: Adding a Textbox
Now we have a new slide in our presentation, let’s add some content to it. We will use the parameter passed in from the user to provide content for a textbox on our slide. First, we need to create a textbox, add it to the slide and position it. Adding an textbox to a slide is more or less the same as adding any type of object. The textbox is added to the slide as a shape. When creating a shape you specify certain information such as size and location on the slide. The code to do this is outlined below.
Line 50 – Obtains the width of the slide and calculates the halfway point, less the width of the textbox that we are going to create
Line 51 – Obtains the height of the slide and calculates the halfway point, less the height of the textbox that we are going to create
Line 53 is where the actual creation of the textbox takes place. The slide that we have added to the presentation has a Shapes collection object. This object has methods for adding different shapes to the slide. We use the AddTextBox
Parameter1 – Specifies the orientation for the text
Parameter2 – The x location for the textbox
Parameter3 – The y location for the textbox
Parameter4 – The width of the textbox shape
Parameter5 – The height of the textbox shape
The AddTextBox method returns a textbox object. We use this object to configure the alignment of the text and the text content itself.
Finishing Up: Save & Quit
The final steps are to save the presentation and then quit of the PowerPoint application object.
The SaveAs method takes three parameters outlined below.
Parameter1 – The filename including path to save to
Parameter2 – The filetype to save the presentation as
Parameter3 – Whether to embed TrueType fonts
Moment of Truth: Testing
The time has come to test our program. Setup a string to pass as a parameter to the program in the projects properties and run in debug mode. Once the program exits you will see the new PowerPoint presentation. Open it and you will see a single slide with your text on it. If you close the PowerPoint presentation and run the program again, you will now find two slides, each with your text on.