Enabling and Disabling Automatic Page Resizing Programmatically in Visio 2010
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
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.
This Visual How To includes two code samples:
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
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
Now, use the following procedure to publish the add-in to Visio 2010.
To publish the add-in to Visio 2010
Now, test the solution you created in Visio 2010.
To test the add-in solution in Visio 2010
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
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.