Sample macro to fit drawing to page in Visio (305461)



The information in this article applies to:

  • Microsoft Office Visio Professional 2003
  • Microsoft Office Visio Standard 2003
  • Microsoft Visio 2002 Professional
  • Microsoft Visio 2002 Standard

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 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("Width").Result("in")
   height = visShape.Cells("Height").Result("in")
    
    'Set the page width and height.
    ThisDocument.Application.ActivePage.PageSheet.Cells _
       ("PageWidth").Formula = width
    ThisDocument.Application.ActivePage.PageSheet.Cells _
       ("PageHeight").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.

REFERENCES

For more information about how to use the sample code in this article, click the following article number to view the article in the Microsoft Knowledge Base:

297304 How to run sample code from Knowledge Base articles

For the latest information, news, resources, downloadable files, and more for the Visio developer, visit the following Microsoft Web site:

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto KB305461