Creating an Addin for Power Point

1. Create a new PowerPoint 2007 add-in project.

2. In Solution Explorer, right-click the project, point to Add, and then click User Control.

3. In the New Item dialog box, ensure that User Control is selected, leaving the default name UserControl1, and then click Add.

3. Add a Label control to the user control, and change the text to View.

4. Add a ComboBox control below the label.

5. Double-click the user control, and add the code in Listing 14.23 to the Load event of UserControl1.

Listing 14.23. Filling a ComboBox

Private Sub UserControl1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load

With Me.ComboBox1.Items .Add("Normal") .Add("Notes Master") .Add("Handout Master") .Add("Slide Master") End With

End Sub

Next, add the code in Listing 14.24 to the SelectedIndexChanged event handler of the combo box. You'll create the ChangeView method next.

Listing 14.24. Passing a selected item to the ChangeView method

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As _ System.Object, ByVal e As System.EventArgs) Handles _ ComboBox1.SelectedIndexChanged

Globals.ThisAddIn.ChangeView(Me.ComboBox1.Text)

End Sub

Now you can add the ChangeView method to the Addin class, and also add code to the Startup event handler to add the user control to the CustomTaskPanes collection. One difference from the actions pane here is that you must explicitly set the Visible property to True to make the task pane visible. Add the code in Listing 14.25 to the Addin class.

Listing 14.25. Displaying the user control on the Custom task pane Dim myControl As UserControl1

Private Sub ThisAddIn_Startup(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Startup myControl = New UserControl1

Me.CustomTaskPanes.Add(myControl, "My Custom Task Pane") _ .Visible = True

Friend Sub ChangeView(ByVal View As String)

Dim ViewType As Integer

Select Case View Case "Normal"

ViewType = PowerPoint.PpViewType.ppViewNormal Case "Notes Master"

ViewType = PowerPoint.PpViewType.ppViewNotesMaster Case "Handout Master"

ViewType = PowerPoint.PpViewType.ppViewHandoutMaster Case "Slide Master"

ViewType = PowerPoint.PpViewType.ppViewSlideMaster End Select

Me.Application.ActiveWindow.ViewType = ViewType End Sub

When you press F5 to run this code, the Custom task pane that contains the combo box becomes visible, as shown in Figure 14.34. You can select the desired master view. You can change the view equally as easily using the Ribbon's View tab, but because a context-sensitive tab becomes visible for each master view, you must continuously switch back to the View tab to see the next view. Showing these options on the Custom task pane eliminates the need to context-switch.

0 0

Post a comment