Creating a Ribbon

30 downloads 354 Views 7MB Size Report
Let's look at how to create and customize a Ribbon menu for Microsoft Office. PowerPoint 2007 using Visual Studio 2008 and VSTO programming. On clicking  ...
Microsoft Office Powerpoint, Visio, and Project Programming

Creating a Ribbon Let's look at how to create and customize a Ribbon menu for Microsoft Office PowerPoint 2007 using Visual Studio 2008 and VSTO programming. On clicking the button placed in the Ribbon, the custom task pane will be displayed. 1. Open Visual Studio 2008 and create a new solution, as described earlier. To add a Ribbon to your solution, right-click on the project name. From context menu, click on Add | New Item…, as shown in the following screenshot:

[ 214 ]

Chapter 6

2. Next, select Ribbon (Visual Designer) and name the Ribbon Ribbon1.cs.

3. On adding the Ribbon to the solution, the Ribbon appears as shown in the following screenshot:

[ 215 ]

Microsoft Office Powerpoint, Visio, and Project Programming

4. Drag-and-drop the control into the Ribbon, as shown in the following screenshot (the button control is added to the Ribbon):

5. Next, you need to add User Control to the solution, in order to develop a custom task pane for PowerPoint 2007. Again, right-click on the project name. From the context menu, click on Add | New Item…, as shown in the following screenshot:

[ 216 ]

Chapter 6

6. Next, select the User Control, and name it UserControl1.cs.

7. Open the UserControl1.cs file, and add the following namespace references: using System.Windows.Forms; using Office = Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint;

8. Open the Ribbon1.cs file, and add the following namespace references: using System.Windows.Forms; using Office = Microsoft.Office.Core; using PowerPoint = Microsoft.Office.Interop.PowerPoint;

9. Next, add the instance of the User Control and custom task pane to the Ribbon code file: // Usercontrol instance to access from the Ribbon private UserControl1 PacktUserControl; // Instance for Custom Task Pane to create private Microsoft.Office.Tools.CustomTaskPane PacktCustomPane;

[ 217 ]

Microsoft Office Powerpoint, Visio, and Project Programming

10. Next, write the following code to show the custom task pane in the button click event of the Ribbon menu: private void RibbonButton1_Click(object sender, RibbonControlEventArgs e) { // Initializing the UserControl in the ribbon PacktUserControl = new UserControl1(); // Add the UserControl to the custom task pane PacktCustomPane = Globals.ThisAddIn.CustomTaskPanes. Add(PacktUserControl, "Calendar"); // Set the custom task pane to visible PacktCustomPane.Visible = true; }

11. On the ValueChanged event of the dateTimePicker , we are writing the code to insert the selected date. private void dateTimePicker1_ValueChanged(object sender, EventArgs e) { try { // Getting the active presentation slide PowerPoint.Slide PckTSlide = Globals.ThisAddIn. Application.ActivePresentation.Slides[1]; // Set the presentation type like text or image imsert option PacktTextShape = PckTSlide.Shapes.AddTextbox(Office. MsoTextOrientation.msoTextOrientationHorizontal, 50, 100, 600, 50); // Set the text value as selected date time PacktTextShape.TextFrame.TextRange.Text = dateTimePicker1. Value.ToString(); // Font style properties PacktTextShape.TextFrame.TextRange.Font.Size = 48; PacktTextShape.TextFrame.TextRange.Font.Color.RGB = Color. DarkViolet.ToArgb(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }

[ 218 ]