Microsoft KB Archive/305461

= Sample macro to fit drawing to page in Visio =

Article ID: 305461

Article Last Modified on 11/28/2006

-

APPLIES TO


 * Microsoft Office Visio Professional 2007
 * Microsoft Office Visio Standard 2007
 * Microsoft Office Visio Professional 2003
 * Microsoft Office Visio Standard 2003
 * Microsoft Visio 2002 Professional Edition
 * Microsoft Visio 2002 Standard Edition

-



This article was previously published under Q305461





For a Microsoft Visio 2000 version of this article, see 275589.



SUMMARY
This article contains a sample Microsoft Visual Basic for Applications macro (Sub procedure) that resizes any page in a Microsoft Office Visio project to fit the drawing on the page.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. The following sample Visual Basic macro resizes any page in a project to fit the drawing on the page.

You can use this macro if you want to eliminate extra viewable areas on one or more pages in the project.
 * 1) In your current Visio project, press ALT+F11 to start the Visual Basic Editor.
 * 2) On the Insert menu, click Module.
 * 3) Click View, and then click Code.
 * 4) Add the following code to the module:

Public Sub FitPageToDrawing

Dim visSelection As Visio.Selection Dim visShape As Visio.Shape Dim width As Double Dim height As Double 'Select all the objects on the page. ThisDocument.Application.ActiveWindow.SelectAll 'Get the current selection. Set visSelection = ThisDocument.Application.ActiveWindow.Selection 'Group the selection. visSelection.Group 'Get the new selection from the result of the group operation. Set visSelection = ThisDocument.Application.ActiveWindow.Selection 'Get the only shape from the selection. Set visShape = visSelection(1)

'Get the width and height of the group. width = visShape.Cells(&quot;Width&quot;).Result(&quot;in&quot;) height = visShape.Cells(&quot;Height&quot;).Result(&quot;in&quot;) 'Set the page width and height. ThisDocument.Application.ActivePage.PageSheet.Cells _ (&quot;PageWidth&quot;).Formula = width ThisDocument.Application.ActivePage.PageSheet.Cells _ (&quot;PageHeight&quot;).Formula = height 'Ungroup the selection. visShape.Ungroup

'Center the drawing. ThisDocument.Application.DoCmd visCmdCenterDrawing

End Sub NOTE: You may have to run the macro several times to get the expected results.

