Creating Slide Filters in PowerPoint 2007 Using Visual Studio Tools for the Office System (3.0)

Summary: Learn to set up slide filters based on criteria and then filter by using a customized Office Fluent Ribbon. (11 printed pages)

Steve Fox, Microsoft Corporation

November 2007

Applies to: 2007 Microsoft Office System, Microsoft Visual Studio Tools for the Microsoft Office System (3.0), Microsoft Office PowerPoint 2007, Microsoft Visual Studio 2008

Contents

  • Scenario Overview

  • Getting Started

  • Creating Custom Ribbons

  • Adding Events to Custom Ribbons

  • Programmatically Adding Comments

  • Programmatically Hiding or Showing Comments

  • Testing the Add-in

  • Deploying PowerPoint Add-Ins

  • Conclusion

  • Additional Resources

Scenario Overview

I’m in a hotel in Germany, on the outskirts of Munich, and it’s 3:30am. I’m too jet-lagged to read my new spy novel, and because my German is limited, I can't understand anything on the TV. So, I sit in a chair with my laptop, open Microsoft Visual Studio 2008. You see, I have this problem I’ve been wanting to solve: I spend a lot of professional time presenting to different audiences. I want to use the same set of slides but adjust them or filter them depending on the audience that I'm presenting to. I want to do this programmatically so I don't have to tweak and sort the same slides in the deck before each presentation.

As I get into the coding, it’s not too bad, with only one major hurdle: finding the hide/show property for a specific slide. After a quick IM exchange with my friend Ken Getz, we find the answer and the rest is a snap. Now I can create an application, which does two things:

  • Add a tag (by using a slide comment) to a specific slide.

  • Filter the slides to display based on the tags.

This article describes the app that I created that morning in Munich. You can use the app to organize a large slide deck by audience; you can then filter that slide deck based on the audience you want to show the deck to. The app uses a customized Microsoft Office Fluent Ribbon.

I built this Microsoft Office PowerPoint 2007 application to filter based on the audiences that I present to: developers, architects, and business decision makers. However, you should adapt this solution to your own audiences. For example, if you want to create a deck that has multiple technical levels, such as 100, 200, or 300, you could include all levels in your presentation and then filter based on what level you want to present.

Getting Started

To create this solution you need:

  • Microsoft Visual Studio 2008

  • Microsoft Visual Studio Tools for the Microsoft Office system (3.0)

  • Windows Vista (other Windows operating systems will work)

  • Microsoft Office PowerPoint 2007

  • 2007 Microsoft Office Program Interop Assemblies

After you set up your environment, open Visual Studio 2008. Then you need to create a Visual Studio Tools for Microsoft Office project.

To create a Visual Studio Tools for Microsoft Office project

  1. Start Microsoft Visual Studio 2008.

  2. On the File menu, point to New, and then click Project.

  3. In the New Project dialog box, expand the Visual Basic or Visual C# node to view the project types. Then expand the Office node and select 2007 Add-ins.

  4. In the Templates pane, select PowerPoint Add-in.

  5. Type SlideDisplay in the Name box and click OK.

  6. If you are using Visual Basic, in Solution Explorer, click Show All Files.

  7. In Solution Explorer, expand the References node.

This creates a Visual Studio Tools for Office project that is a PowerPoint application-level add-in. Application-level add-ins load when you launch the application.

Creating Custom Ribbons

To handle the events, I created a custom Office Fluent Ribbon, named Slide Mgmt. In the custom Ribbon, I added two groups: Slide Display Mgmt and Add Comment Tag. Within the Slide Display Mgmt group, I created toggle buttons for each audience that show or display a slide according to the button state. Within the Add Comment Tag group, I created toggle buttons for each audience that added a "tag" to each slide according to the button state. Lastly, I added a Show All Slides check box to provide the option to display all slides regardless of the audience. Figure 1 shows the custom Ribbon in the context of the Visual Studio 2008 development environment.

Figure 1. Custom Ribbon in Visual Studio 2008

Custom Ribbon in Visual Studio 2008

As you can see in Figure 1, I also associated images with my buttons. Although optional, images help me differentiate buttons.

To add the custom Ribbon

  1. In Visual Studio, right-click your project, point to Add, and then click Add New Item.

  2. In the Add New Item window, click Ribbon (Visual Designer).

  3. Type a name for your custom Ribbon.

Using the Ribbon Designer in Visual Studio, you can drag and drop the Ribbon elements as described previously. You can also set properties such as button images. After you add your custom Ribbon, the next step is to add events to each of the Ribbon elements.

Adding Events to Custom Ribbons

Before adding events, be sure you have the following references at the top of your class file. Most of these are added by default. For example, I do not use LINQ in this sample, but the reference is present. However, I added a reference to Microsoft.Office.Interop.PowerPoint to simplify the code.

// Add references to namespaces.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

After creating the custom Ribbon, you need to add click events to each of the buttons. When you do this, your project opens a project class file that corresponds to your Ribbon. For example, in my example, I named my custom Office Fluent Ribbon SlideDisplay, so most of the code I wrote appears in the class file called SlideDisplay.cs.

At the top of the SlideDisplay class are a number of key variables I used throughout my events. The following example shows these variables, which are essentially the parameters required to add a new comment to a slide. I used comments as tags to identify a slide according to audience.

// Identifying key variables.

namespace PPTSlideToggle
{
    public partial class SlideDisplay : PowerPointRibbon
    {

        string deckAuthor = "Steve Fox";
        string authorInitials = "SCF";
        float topPos = 0;
        float leftPos = 0;
       ...
     }
}

The two groups of buttons on the custom Office Fluent Ribbon correspond to the primary features in the application. In this next section, I walk through the code for the two primary features. I add a comment (tag) and then hide and show each slide based on the comment.

Programmatically Adding Comments

You can add a comment manually, or you can use the button on the custom Ribbon. The important thing in this solution is the specifics of the comment. That is, the filtering code looks for specific tags. In this case, it looks for comments that contain Dev, BDM, or Arch (which represent my three audiences) and then hides or shows the slide according to the chosen value. The filtering code also looks for comments authored by the name you associate with the string; otherwise, the slides are not filtered. Therefore, when we add the comments (or tags), we need to make sure that we set the properties appropriately. In my case, this meant Steve Fox as the author, SCF as the author’s initials, and, again, either Dev, BDM, or Arch as the actual text within the comment.

If you choose to implement the custom Ribbon element rather than add the comments to your slide manually, you can associate a hard-coded comment with a specific button and then call a method (in this case the addCommentTag method) and pass it the hard-coded comment text. For each of the buttons in the Add Comment Tag button group, the code is similar. The following code example shows how to add a Dev tag and associates a hard-coded string with the button. It then calls the addCommentTag method, passing the string comment as a parameter. I used a toggle button, but you can use a regular button or a check box, depending on how you design the application.

// Adding a comment button event.
private void btnDevTag_Click(object sender, RibbonControlEventArgs e)
  {
     string commentText = "Dev";
     addCommentTag(commentText);
  }

The addCommentTag method then uses the comment text to add a new comment to the slide that is currently active. That is, the slide that you selected in the PowerPoint presentation. You could also choose to add a new slide and then add the comment with that. To do this, instead of setting the slide to the actively selected slide you use the Add method to add a slide and then use the same code before and after this line of code to add the comment to the slide as the following example shows.

// Adding a new slide.

PowerPoint.Slide slide = prez.Slides.Add(prez.Slides.Count + 1, 
  PowerPoint.PpSlideLayout.ppLayoutText);

That said, I felt that it was logical to add the slide after the actively selected slide. Note that in the following code example, I create a Presentation instance named prez and then a Slide instance named slide, which I needed to cast because the active slide is of type object. Then, I added the comments to the active slide by using the Add method.

// Add comment method.

public void addCommentTag(string commentTagText)
  {   
    PowerPoint.Presentation prez = Globals.ThisAddIn.Application.ActivePresentation;
    PowerPoint.Slide slide = prez.Windows.Application.ActiveWindow.View.Slide as PowerPoint.Slide;
    slide.Comments.Add(topPos, leftPos, deckAuthor, authorInitials, commentTagText);
  }

With this functionality now built, let us discuss how to hide and show the comments that you add to the presentation.

Programmatically Hiding or Showing Comments

After you add your comment tags, you can change the property on the slide that hosts the comment to either show or hide the comment. Using the custom toggle buttons (or the controls that you choose) as triggers for hiding and showing these comments, I added three events to each of the three buttons in the Slide Display Mgmt button group. Because each of the buttons uses similar code, I provide one example of calling an event from one of the buttons and discuss the conditions and parameters for the event call. In the following code example, I add the comment text in the context of the event. Remember that deckAuthor is a string variable I set outside the context of this click event. I based the condition of the method calls on the label property of the button. If the value of the label property is Show BDM Slides, then the toggle button triggers the hideSlides method. If the label property is Hide BDM Slides, then the toggle button triggers the showSlides method. The method also changes the label of the button.

// Toggle the button.
private void toggleBtnBDM_Click(object sender, RibbonControlEventArgs e)
   {
         string devTag = "BDM";

      if (toggleBtnBDM.Label == "Show BDM Slides")
         {
            toggleBtnBDM.Label = "Hide BDM Slides";
            hideSlides(deckAuthor, devTag);
         }
      else if (toggleBtnBDM.Label == "Hide BDM Slides")
         {
            toggleBtnBDM.Label = "Show BDM Slides";
            showSlides(deckAuthor, devTag);
         }
    }

When you call the hideSlides method, it passes the author parameter and devTag parameter. These two parameters are used as conditions to set the slide to hide. They set the Hidden property to msoFalse. The following code example illustrates the call to the hideSlides method.

// The hideSlides method.

public void hideSlides(string deckAuthor, string devComment)
  {
      foreach (PowerPoint.Slide slide in Globals.ThisAddIn.Application.ActivePresentation.Slides)
        {
          foreach (PowerPoint.Comment comment in slide.Comments)
            {
             if (comment.Author == deckAuthor)
              {
                if (comment.Text == devComment)
                  {
                   slide.SlideShowTransition.Hidden = Microsoft.Office.Core.MsoTriState.msoFalse;
                  }
              }
            }
        }
   }

Conversely, when the showSlides method is called the Hidden property is set to msoTrue. The next code example is the showSlides method.

// The showSlides method.
public void showSlides(string deckAuthor, string devComment)
    {
      foreach (PowerPoint.Slide slide in Globals.ThisAddIn.Application.ActivePresentation.Slides)
        {
           foreach (PowerPoint.Comment comment in slide.Comments)
             {
               if (comment.Author == deckAuthor)
                 {
                   if (comment.Text == devComment)
                    {
                    slide.SlideShowTransition.Hidden = Microsoft.Office.Core.MsoTriState.msoTrue;
                    }
                 }
             }
         }
    }

Finally, I added a check box to show all of the slides. The purpose of this shortcut is to reset the Show property to start over quickly. The chkBoxShowAll_Click event resets the labels of the buttons in the Slide Display Mgmt group and then calls the showAllSlides method.

// Adding the Show All Slides event handler.
private void chkBoxShowAll_Click(object sender, RibbonControlEventArgs e)
  {
     toggleBtnArch.Label = "Hide Arch Slides";
     toggleBtnBDM.Label = "Hide BDM Slides";
     toggleBtnShowDev.Label = "Hide Dev Slides";
     showAllSlides();
  }

The showAllSlides method uses the deckAuthor variable as a condition for setting the Hidden property to false. In the context of my application, this takes any slide with a comment with my name as the author and shows the slide.

// The showAllSlids method.
public void showAllSlides()
  {

     foreach (PowerPoint.Slide slide in Globals.ThisAddIn.Application.ActivePresentation.Slides)
           {
              foreach (PowerPoint.Comment comment in slide.Comments)
              {
                 if (comment.Author == deckAuthor)
                 {
                   slide.SlideShowTransition.Hidden = Microsoft.Office.Core.MsoTriState.msoFalse;
                 }
              }
           }
   }

Testing the Add-in

After you build the application, you can press F5 to build the project. Assuming you use Visual Studio 2008, your application should compile without issues. Note that I did not test this on previous versions of Visual Studio. After compilation, the application launches PowerPoint and creates a slide deck. You can test the application by adding slides, adding comments, and then hiding and showing the slides. In Figure 2, you can see that I added three slides to test my application. The first is tagged as a dev slide, the second is tagged as an architecture slide, and the third is tagged as a business decision maker slide. I also used the dev tag buttons to add comments to each of the slides.

Figure 2. Testing the Features

Feature testing

In my application, I successfully added the comments to the slide and then hid the BDM and Architecture slides in my slide deck. Note that when you hide a slide, the slide number is enclosed with a small square with a diagonal line through it.

Figure 3. Hidden BDM and architecture slides

Hidden slides

Deploying PowerPoint Add-Ins

After you build your application, you need to deploy it. This is straightforward if you want to deploy to your local computer, file share, Web site, or CD.

To deploy your add-in

  1. Right-click your project name and select Publish. This launches the Publish wizard.

  2. Select a folder in which to publish all the prerequisite files and then click Next.

    Figure 4 illustrates the first step in the Publish Wizard.

    Figure 4. Selecting a publishing folder

    Selecting a publishing folder

  3. Next, select the installation path such as a Web site, file share, or CD.

  4. Click Next and then click Finish. This launches the publish process and deploys all of your setup files to your selected location.

In my application, I published my files locally with the forethought of deploying this to a client using a CD. Figure 5 shows the setup files published to my selected folder (a local folder on my PC). I can then copy these files to a CD where others could then click setup.exe to install the add-in to their local client.

Figure 5. Setup Files

Setup files

For more information about creating and deploying PowerPoint Add-Ins, see Creating PowerPoint 2007 Add-Ins by Using Visual Studio 2005 Tools for the Office System SE.

For more information about other deployment options, see Deploying Solutions for 2007 Office System with ClickOnce Using Visual Studio Tools for the Office System (3.0).

Conclusion

This article shows you how to build and deploy a PowerPoint add-in by using Visual Studio Tools for the Office system (3.0). I also wanted to provide you with a fundamental understanding of how you can build an application-level add-in similar to the one discussed here to filter slides for different audiences. The add-in does two things: it adds comments to a selected slide and hides or shows the slides based on the comments added to the slide. You need to modify the code so that it has your own name and initials. You can also change the text of the comments, including adding comments, and filter the comments based on your own needs.

You can find more information about Visual Studio Tools for the Office system (3.0) in Additional Resources.

Acknowledgements

Thanks to Ken Getz for collaborating with me and helping me find the slide hide/show property while I was jet-lagged in Munich. During our e-mail/IM exchanges he, like me, was wondering just what I was doing up at that hour!

Additional Resources