Part 3: Automating PowerPoint 2010 and creating slides, shapes, connectors, and pictures by using data from the Visio 2010 shapes

Office 2010

Summary:  The third article in a series of four articles about how to export Microsoft Visio 2010 shapes to Microsoft PowerPoint 2010. Part 3 discusses how to create new PowerPoint shapes by using data that is contained in ShapeConversion objects.

Last modified: June 12, 2012

Applies to: Office 2010 | PowerPoint 2010 | SharePoint Server 2010 | Visio 2010 | Visio Premium 2010 | Visual Studio 2010

In this article
Controlling PowerPoint from another application
Building PowerPoint Connectors, Pictures, and Shapes
Read the Other Articles in this Series

Published:  June 2012

Provided by:  Eric Schmidt, Microsoft Corporation

Contents

Download the code

The Exporting Visio 2010 Diagrams to PowerPoint 2010 add-in accomplishes its task by controlling the PowerPoint application from Visio. To do this, it captures a reference to a PowerPoint session by creating a new instance of PowerPoint.

Note Note

Automating one application from another is only one way that you can accomplish this task. You can also create PowerPoint 2010 files using the Open XML SDK 2.0 for Microsoft Office. For more information about the , see About the Open XML SDK 2.0 for Microsoft Office.

As mentioned in Part 2: Writing the classes and methods that convert and export Visio 2010 diagrams to PowerPoint 2010, the OpenPPTSession method of the ThisAddIn class returns a new Presentation object to the calling code. Before it can create a new presentation, it must first create an instance of the Application class. Then, it uses the Add(MsoTriState) method to create a new Presentation object.

The AddPPTSlide method of the ThisAddIn class takes a Presentation object as an argument and returns a Slide object to the calling code by using the AddSlide(Int32, CustomLayout) method of the presentation passed in as an argument.

The AddSlide(Int32, CustomLayout) method has two required parameters:

  • Index, an integer that determines where the new slide is inserted in the Slides collection

  • pCustomLayout, which specifies the slide layout from the Slide Master to use for the new slide

The Exporting Visio 2010 Diagrams to PowerPoint 2010 add-in attempts to use the Blank slide layout of the default PowerPoint template for the exported Visio pages. The AddPPTSlide method first checks whether the seventh layout in the CustomLayouts collection (the Blank layout) exists and, if so, it captures a reference to that layout in the pptLayout variable. Otherwise, if the CustomLayouts collection does not include the seventh item, it instead returns the first item in the collection.

Below the definition of the ExportAllPages method of the ThisAddIn class in the ThisAddIn.vb or ThisAddIn.cs file, add the following code, which creates a new PowerPoint application object, creates a new presentation, and adds a slide to the presentation.


public PowerPoint.Presentation OpenPPTSession()
{

    // Create a PowerPoint application object.
    PowerPoint.Application appPPT = new PowerPoint.Application();

    // Create a new PowerPoint presentation.
    PowerPoint.Presentation pptPreso = appPPT.Presentations.Add();

        return pptPreso;
    }

public PowerPoint.Slide AddPPTSlide(PowerPoint.Presentation pptPreso)
{

    // Define pptLayout as the "Blank" layout of the default presentation template.
    // If another template is set as default, select the first layout.
    PowerPoint.CustomLayout pptLayout = default(PowerPoint.CustomLayout);
    if ((pptPreso.SlideMaster.CustomLayouts._Index(7) == null))
    {
        pptLayout = pptPreso.SlideMaster.CustomLayouts._Index(1);
    }
    else
    {
        pptLayout = pptPreso.SlideMaster.CustomLayouts._Index(7);
    }

    // Create newSlide by using pptLayout.
    PowerPoint.Slide newSlide = 
        pptPreso.Slides.AddSlide((pptPreso.Slides.Count + 1), pptLayout);

    return newSlide;
}

As mentioned earlier in Part 2: Writing the classes and methods that convert and export Visio 2010 diagrams to PowerPoint 2010, the Main method of the ThisAddIn class can convert a Visio shape to a PowerPoint shape in one of three ways: as a connector, as a picture, or as a built-in PowerPoint shape. The Main method uses the ShapeConversion.pptShapeType property to determine how it converts each shape. Depending on that value, the Main method calls either the ConstructPPTConnector method, the CopyPasteVisioToPPT method, or the ConstructPPTShape method of the ThisAddIn class.

The first method, ConstructPPTConnector, takes a ShapeConversion object as its parameter and constructs a new PowerPoint connector on the PowerPoint slide. It uses the AddConnector(MsoConnectorType, Single, Single, Single, Single) method to add a new shape to the Shapes collection of the pptBlankSlide object. The AddConnector(MsoConnectorType, Single, Single, Single, Single) method takes five parameters:

  • Type—A constant from the MsoConnectorType enumeration.

  • BeginX and BeginY—16-bit signed integers that specify the beginning point of the connector on the slide.

  • EndX and EndY—16-bit signed integers that specify the ending point of the connector on the slide.

The ConstructPPTConnector method creates a new elbow connector on the PowerPoint slide by using the ShapeConversion.pptConnectorBeginX, ShapeConversion.pptConnectorBeginY, ShapeConversion.pptConnectorEndX, and ShapeConversion.pptConnectorEndY properties to specify the endpoints of the PowerPoint connector. After adding the connector to the slide, the method sets the Name property to match the name of the original Visio connector, which is needed to connect shapes and connectors later.

In the ThisAddIn.vb or ThisAddIn.cs file, below the definition of the Main method, add the following code, which creates new elbow connector shapes on the PowerPoint slide (pptBlankSlide object).


private void ConstructPPTConnector(ShapeConversion currShapeConversion)
{
    // Create the PowerPoint connector shape.
    PowerPoint.Shape pptConnector = default(PowerPoint.Shape);
    pptConnector = pptBlankSlide.Shapes.AddConnector(
        Microsoft.Office.Core.MsoConnectorType.msoConnectorElbow,
        currShapeConversion.pptConnectorBeginX, 
        currShapeConversion.pptConnectorBeginY,
        currShapeConversion.pptConnectorEndX,
        currShapeConversion.pptConnectorEndY);

    // Set the name of the connector shape.
    pptConnector.Name = currShapeConversion.pptShapeName;
}

The most difficult case that the Exporting Visio 2010 Diagrams to PowerPoint 2010 add-in must handle is when there is no PowerPoint analog to a Visio shape. In this circumstance, the add-in creates an Enhanced Metafile picture of the Visio shape on the PowerPoint slide. The CopyPasteVisioToPPT method takes a Visio.Shape object and a ShapeConversion object as arguments, and makes a copy of the Visio.Shape object on the PowerPoint slide.

When a Visio shape is copied and pasted as a picture into another program, the text is copied as part of the picture too. Unfortunately, this means that the text can no longer be edited or formatted. If a Visio shape to copy has text, the CopyPasteVisioToPPT method cuts the text and stores it in a hidden RichTextBox object so that the picture on the PowerPoint slide contains only the shape image without text. (Once the method pastes and formats the picture on the PowerPoint slide, it replaces the text in the Visio shape from the RichTextBox.)

The CopyPasteVisioToPPT method pastes the Visio shape onto the PowerPoint slide by using the PasteSpecial(PpPasteDataType, MsoTriState, String, Int32, String, MsoTriState) method, which returns a ShapeRange collection. The method then captures a reference to the first item in the ShapeRange collection—the picture itself—in a Shape object. Next, the method resizes, positions, and renames the picture, based on the values that are contained in the currShapeConversion object.

Also if the Visio shape contains text, the CopyPasteVisioToPPT method calls the AddTextOverlay method to add the text in a text box on top of the picture.

In the ThisAddIn.vb or ThisAddIn.cs file, below the definition for the ConstructPPTConnector method, add the following code, which copies a shape from Visio, pastes it onto the PowerPoint slide, and resizes the shape.


private void CopyPasteVisioToPPT(Visio.Shape currVisioShape, 
    ShapeConversion currShapeConversion)
{
    PowerPoint.ShapeRange pptPic;
    PowerPoint.Shape pptShape;
    RichTextBox rtfBox = new RichTextBox();
    Visio.Characters vsoCharacters = currVisioShape.Characters;

    // Remove text from the shape to exclude it from the picture.
    if (currShapeConversion.pptShapeText != null)
    {
        vsoCharacters.Cut();
        rtfBox.Paste();
        currVisioShape.get_CellsU("Para.Bullet").FormulaU = "0";
    }

    // Paste the picture onto the PowerPoint slide 
    // and capture a reference to it.
    currVisioShape.Copy();
    pptPic = pptBlankSlide.Shapes.PasteSpecial(
        PowerPoint.PpPasteDataType.ppPasteEnhancedMetafile);
    pptShape = pptPic[1];

    // Adjust the dimensions of the shape and change the name.
    pptShape.Name = currShapeConversion.pptShapeName;
    pptShape.Width = currShapeConversion.pptShapeWidth;
    pptShape.Left = currShapeConversion.pptShapeLeft;
    pptShape.Top = currShapeConversion.pptShapeTop;

    if (currShapeConversion.pptShapeWidth == 0)
    {
        pptShape.Height = currShapeConversion.pptShapeHeight;
    }

    // Create a text box over the shape with the shape's text, if any.
    if (string.IsNullOrEmpty(currShapeConversion.pptShapeText) != true)
    {
        AddTextOverlay(currShapeConversion);

        // Cut and paste RTF from the text box back to the Visio shape.
        rtfBox.SelectAll();
        rtfBox.Cut();
        currVisioShape.Characters.Paste();
    }
}

For the third conversion case, the ConstructPPTShape method creates a PowerPoint shape that is equivalent to the Visio shape and then formats the PowerPoint shape to match the Visio shape. The method uses the AddShape(MsoAutoShapeType, Single, Single, Single, Single) method to add a new Shape to the pptBlankSlide object. Because the ShapeConversion class constructor method converts the formatting values of the Visio shape to values that PowerPoint can interpret, the ConstructPPTShape method uses the exposed properties of the ShapeConversion object that is passed in as an argument to build and format the new Shape object.

After adding the Shape to the Shapes collection, the ConstructPPTShape method adds text to the shape, depending on whether the ShapeConversion.pptShapeText property contains a value. If the ShapeConversion.pptShapeText property is not null, the ConstructPPTShape method adds the text to the shape and then formats the text to match the Text 1 color value of the PowerPoint theme. Depending on the text added to the PowerPoint shape, the shape might increase or decrease in height or change position along the y-axis of the slide. To address this, the method resets the Shape object height and distance from the top edge of the slide if the shape is too small. (You can also set the AutoSize property of the TextFrame object that is associated with the Shape object to ppAutoSizeNone. However, this can result in the text becoming unreadable or compressed.

NoteNote

If you want to specify exact color values when you program PowerPoint 2010 using C#, you must first create a Color structure, and then use the ToArgb() method to convert the object to an integer value. The properties and methods in the PowerPoint 2010 primary interop assembly that set and get specific colors accept only integer values that express RGB values.

Also note that you must add the using System.Drawing directive to your code to have access to the Color structure.

Under the CopyPasteVisioToPPT method in the ThisAddIn.vb or ThisAddIn.cs file, add the following code, which uses a ShapeConversion object to create a new PowerPoint shape on the PowerPoint slide.


private void ConstructPPTShape(ShapeConversion currShapeConversion)
{
    // Create a PowerPoint shape on pptBlankSlide.
    PowerPoint.Shape pptShape = pptBlankSlide.Shapes.AddShape(
        (Office.MsoAutoShapeType)currShapeConversion.pptShapeType,
        currShapeConversion.pptShapeLeft,
        currShapeConversion.pptShapeTop,
        currShapeConversion.pptShapeWidth,
        currShapeConversion.pptShapeHeight);

    // Rename and apply fill color to the new PowerPoint shape.
    pptShape.Name = currShapeConversion.pptShapeName;
    pptShape.Fill.Solid();
    pptShape.Fill.ForeColor.RGB = currShapeConversion.pptShapeFillColor;

    // Add shape text, if any.
    if (String.IsNullOrEmpty(currShapeConversion.pptShapeText) != true) {
        pptShape.TextFrame.TextRange.Text = currShapeConversion.pptShapeText;
        pptShape.TextFrame.TextRange.Font.Color.ObjectThemeColor = 
            Office.MsoThemeColorIndex.msoThemeColorText1;

        // Check whether adding text resized the shape.
        if (pptShape.Height < currShapeConversion.pptShapeHeight)
        {

            // Resize the shape if it is too small.
            pptShape.Height = currShapeConversion.pptShapeHeight;
            pptShape.TextFrame.AutoSize = 
                PowerPoint.PpAutoSize.ppAutoSizeNone;
            pptShape.Top = currShapeConversion.pptShapeTop;
        }
    }
}

As mentioned earlier, pictures (and connectors) in PowerPoint do not contain TextFrame objects that you can use to display text on the slide. The Exporting Visio 2010 Diagrams to PowerPoint 2010 add-in addresses this issue by overlaying pictures with a text box that contains the text from the Visio shape. Specifically, the AddTextOverlay method of the ThisAddIn class uses the AddTextbox(MsoTextOrientation, Single, Single, Single, Single) method to create a new text box that uses the left, top, width, and height values of the original shape. (The complete version of the Exporting Visio 2010 Diagrams to PowerPoint 2010 add-in uses the spatial values of the text block of the original Visio shape.) After the text box shape is created, the method adds the text that is contained in the ShapeConversion.pptShapeText property to the text box and formats the text.

Under the ConstructPPTShape method in the ThisAddIn.vb or ThisAddIn.cs file, add the following code, which creates a text box shape over the picture shapes that the CopyPasteVisioToPPT method creates.


private void AddTextOverlay(ShapeConversion currShapeConversion) {
            
    // Create a text box (with text) on top of and centered on the shape.
    PowerPoint.Shape pptShpText = pptBlankSlide.Shapes.AddTextbox(
        Office.MsoTextOrientation.msoTextOrientationHorizontal,
         currShapeConversion.pptShapeLeft,
         currShapeConversion.pptShapeTop,
         currShapeConversion.pptShapeWidth,
         currShapeConversion.pptShapeHeight);

    // Add and format the text in the text box.
    pptShpText.TextFrame.AutoSize = PowerPoint.PpAutoSize.ppAutoSizeNone;
    pptShpText.TextFrame.TextRange.Text = currShapeConversion.pptShapeText;
    pptShpText.TextFrame.TextRange.Font.Color.ObjectThemeColor = 
        Office.MsoThemeColorIndex.msoThemeColorText1;

    // Change the background of the text box to match the slide background 
    //  if the shape is a connector.
    if (currShapeConversion.pptShapeType == 0) {
        pptShpText.TextFrame.TextRange.ParagraphFormat.Alignment = 
            PowerPoint.PpParagraphAlignment.ppAlignCenter;
        pptShpText.Fill.ForeColor.ObjectThemeColor = 
            Office.MsoThemeColorIndex.msoThemeColorBackground1;
    }

    // Reposition the text box after adding the text.
    pptShpText.Left = currShapeConversion.pptShapeLeft;
    pptShpText.Top = currShapeConversion.pptShapeTop;
}

The ConnectShapes method, which is called during the second loop of the Main method of the ThisAddIn class, connects a PowerPoint connector and PowerPoint shape on the slide. The method has a single parameter named visConnect, a Visio.Connect object, which defines a connection between a Visio shape and a Visio connector. If the Visio shape that is returned by the Connect.FromSheet property is a dynamic connector, the method gets a reference to the corresponding PowerPoint shape and connector on the slide. The method then uses the Connect.FromPart property of the Visio.Connect object to determine which end of the connector is connected to the shape. Depending on the end that is connected, the ConnectShapes method calls the BeginConnect(Shape, Int32) or the EndConnect(Shape, Int32) method of the Shape object to attach the connector to the shape.

The method also calls the RerouteConnections() after both ends of the connector are connected. If either end of the connector is not connected, any call made to the RerouteConnections() method raises an exception. Most often, the Connect objects in the Connects collection are arranged so that the first Connect object that references a dynamic connector is the beginning connection, and the next object in the collection is the ending connection. Thus, the ConnectShapes method calls the RerouteConnections() method after the EndConnect(Shape, Int32) method is called.

Below the AddTextOverlay method of the ThisAddIn class in the ThisAddIn.vb or ThisAddIn.cs file, add the following code, which takes a Connect object as an argument and connects two PowerPoint shapes on the PowerPoint slide.


private void ConnectShapes(Visio.Connect visConnect)
{
    // Get the names of the two connected shapes.
    string cnctrName = visConnect.FromSheet.Name;
    string shapeName = visConnect.ToSheet.Name;

    // Check that the connector shape is a Dynamic connector.
    if (cnctrName.ToLower().Contains("connector")) { 

        // Get analog shapes on the PowerPoint slide.
        PowerPoint.Shape pptConnector = pptBlankSlide.Shapes[cnctrName];
        PowerPoint.Shape pptShape = pptBlankSlide.Shapes[shapeName];

        // Get the relationship of the connection point to the connector 
        // and the shape.
        int cnctrEnd = visConnect.FromPart;

        // If the pptConnector begins at the connection point with 
        // the pptShape, use the BeginConnect method.
        if (cnctrEnd == 9)
        {
            pptConnector.ConnectorFormat.BeginConnect(pptShape, 1);
        }

        // Otherwise, connect the end of the pptConnector to the pptShape.
        else {
            pptConnector.ConnectorFormat.EndConnect(pptShape, 1);

            // Reroute the pptConnector to the closest connection point 
            // on the pptShape.
            pptConnector.RerouteConnections();
        }
    }
}
Show:
© 2014 Microsoft