Export (0) Print
Expand All

Enabling and Disabling Automatic Page Resizing Programmatically in Visio 2010

Office 2010

Office Visual How To

Summary:  Learn how to enable and disable the automatic page-resizing feature programmatically in Microsoft Visio 2010 by using the Page.AutoSize property, Page.AutoSizeDrawing method, and Document.DiagramServicesEnabled property.

Last modified: September 12, 2012

Applies to: Office 2010 | VBA | Visio 2010 | Visual Studio

Published:   January 2011

Provided by:   Saul Candib, Microsoft Corporation

Overview

This Visual How To explains how to use the Page.AutoSize property to control whether Microsoft Visio 2010 automatically resizes the drawing page when shapes are added to it in the user interface, and how to use the Page.AutoSizeDrawing method to resize the page at any time. Both members are included in the Visual Basic for Applications (VBA) object model. This Visual How To also shows how to set the Document.DiagramServicesEnabled property value to enable this page-resizing feature for shapes that you manipulate programmatically.

When the page Auto Size feature in Visio 2010 is enabled, Visio 2010 automatically adds or removes printer-paper-sized tiles to the drawing, as necessary, to best fit the drawing to the shapes on the page. Users can turn this feature on and off in the user interface (UI) by clicking Auto Size in the Page Setup group on the Design tab. As a developer, you can control this setting programmatically, both for shapes that the user adds and removes manually, and for shapes that are added and removed programmatically. You can also resize the page dynamically at any time to best fit the drawing to the shapes on the page.

In the Visual Basic Editor, you can use the Page.AutoSize property, which has a Boolean value, to toggle automatic page resizing on and off. Changing this value from false to true does not automatically resize the page immediately. However, you can call the Page.AutoSizeDrawing method to resize the page after you change the AutoSize property value, or at any other time. Page.AutoSizeDrawing resizes the drawing page regardless of the AutoSize property setting.

If you manipulate shapes programmatically and you want your code to automatically resize pages, you must set the Document.DiagramServicesEnabled property to an integer value that includes the constant visServiceAutoSizePage (from the VisDiagramServices enumeration). This Visual How To uses a minimal C# Visio 2010 add-in project in Microsoft Visual Studio 2010 to show how to use the DiagramServicesEnabled property to enable this feature for shapes that are manipulated programmatically.

Code It

This Visual How To includes two code samples:

  • A VBA procedure that enables automatic page resizing in the user interface and resizes the currently active page.

  • A C# sample that is inserted into a Visio 2010 add-in project created in Visual Studio 2010.

Enabling Page Auto Size in the UI and Resizing the Page by Using VBA

Use the following steps to create and run the EnablePageAutoSize procedure, as shown in the following code example. However, to better demonstrate the effect of the code, first create a blank Visio drawing, turn off Auto Size in the UI, and then drag a single shape onto the drawing page in such a way that it extends partially beyond the edge of the page. Notice that the page does not resize.

To enable automatic page resizing in the UI and resize the page by using VBA

  1. In Visio 2010, press Alt+F11 to open the Visual Basic Editor window.

  2. In the Project window, double-click the ThisDocument object.

  3. In the code pane, paste the following procedure.

    Public Sub EnablePageAutoSize()
    
        ActivePage.AutoSize = True
        ActivePage.AutoSizeDrawing
            
    End Sub
    
  4. Click anywhere within the procedure, and then press F5.

After you run the code, reexamine the drawing and note both that Auto Size is now enabled in the UI, and that Visio 2010 has resized the page by adding another printer-paper-sized tile to encompass the entire shape.

Enabling Automatic Page Resizing in a C# Visio Add-In

Use the following procedures to create, publish, and test a minimal Visio 2010 add-in in Visual Studio 2010 that adds a shape to the page and enables automatic page resizing for programmatic manipulation of shapes.

To create a C# Visio 2010 add-in project in Visual Studio 2010

  1. On the Start Page in Visual Studio 2010, click New Project.

  2. In the Installed Templates list, expand the Visual C# node, and then expand the Office node.

  3. Click 2010, and then select Visio 2010 Add-in.

  4. Name the project, and either accept the default location or browse to another location in which to save the project.

  5. Click OK.

  6. In the ThisAddIn_Startup method, paste the following code.

    this.Application.ActivePage.DrawRectangle(4,4,12,8);
    
  7. On the Build menu, click Build Solution.

  8. Save the project.

Now, use the following procedure to publish the add-in to Visio 2010.

To publish the add-in to Visio 2010

  1. In Solution Explorer, right-click the project name, and then click Publish.

  2. In the first Publish Wizard page, browse to the location where you want to publish your solution, or just accept the default location, and then click Finish.

  3. Save the project.

Now, test the solution you created in Visio 2010.

To test the add-in solution in Visio 2010

  1. Open Visio 2010 and create a new drawing from the Basic Diagram (US Units) template.

  2. On the Developer tab, click COM Add-Ins.

  3. In the Add-Ins available list, check the box for the add-in you created, and then click OK.

The add-in runs and draws a rectangle on the page. However, Visio does not automatically resize the page. As a result, the rectangle extends beyond the page boundaries. This is the case whether or not Auto Size is selected on the Design tab in the UI.

To fix this, you must enable diagram services in the document, which requires that you add just one line to the solution code. The following procedure describes how to do that and test the results.

To revise and republish the add-in solution and retest it in Visio 2010

  1. In Visual Studio 2010, in the ThisAddIn_Startup method in your solution, above the existing code, paste the following line of code.

    this.Application.ActiveDocument.DiagramServicesEnabled = (int)Visio.VisDiagramServices.visServiceAutoSizePage;
    
  2. On the Build menu, click Rebuild Solution.

  3. In Solution Explorer, right-click the project name and then click Publish.

  4. In the Publish Wizard, click Finish.

  5. Save the project.

  6. In Visio 2010, on the Quick Access toolbar, click Undo.

  7. On the Developer tab, click Com Add-Ins.

    Note Note

    The Developer tab is not displayed by default. To display the Developer tab, click File, click Options, click Advanced, and then scroll down to General and select Run in Developer Mode.

  8. In the Add-Ins available list, clear the check box for the add-in you created, and then click OK.

    This step and the next have the effect of restarting your add-in. This is necessary because your code is in the ThisAddIn_Startup method, which runs only when the add-in starts.

  9. In the Add-Ins available list, select the check box again for the add-in you created, and then click OK again.

Your add-in runs and draws a rectangle on the page. This time, however, Visio resizes the page by adding another printer-sized tile. The rectangle is completely included in the drawing.

See It

Watch the video

Watch video

Length: 09:03

Explore It
Show:
© 2014 Microsoft