Fit All Pages to Window in a Visio Document
If you create Visio documents with lots of pages, you probably like to quickly flip through all of them to check for visual consistency.
But if each page has been zoomed or panned, it can be frustrating to have to click Fit to Window for each page.
But with a little macro code, you can make your life a lot easier!
If you’ve spent any time on this site at all, it’s no secret that Visio can be programmed and automated on several levels. In addition to the ShapeSheet for creating SmartShapes, Visio includes a VBA environment for quickly programming macros in the Visual Basic language. VBA is great for helping out with repetitive tasks that become tedious and even interrupt our workflow.
When editing a long Visio document, I like to flip through all of the pages, from start to finish, to see if anything looks out of place. Sometimes I place reminder shapes on pages with unfinished work. A simple technique is to draw a rectangle, formatted with big, fat red text that says TODO. This is easy to spot when flipping pages and reminds you that you work is not done.
The trouble is that pages are often zoomed and panned to some odd corner–where you last left them! You can quickly flip through the pages in Print Preview using the Previous/Next Tile buttons. Similarly, you can turn pages in the drawing window, using keyboard shortcuts like [Ctrl] + [Page Down] and [Ctrl] + [Page Up] (or using your ThinkPad’s page-forward and page-backward keys) .
But you will still have to stop and set the zoom level to “Fit to Window'”. This messes up your rhythm, and makes it hard for your pattern-detecting abilities to gel.
You can speed up Fit to Window by pressing [Ctrl] + [W] in pre-Visio 2010, or [Ctrl] + [Shift] + [W] in Visio 2010. This zooms your page so that it fits perfectly in the drawing window. But for documents with more than five pages or so, this still gets in the way of a fluid and rapid scan.
So I use the VBA code at the end of this article to automate the process of fitting every page in my document to the drawing window. At the press of a button, all of my pages are properly zoomed. I can then rapidly flip through each page and check for consistency. I store the code in a stencil that is saved in my Favorites folder. This is easily accessible from within Visio, via the More Shapes menu in Visio 2010, or under File in older versions.
If you are unfamiliar with VBA and macro programming, see this article: Just for starters. It introduces you to Visio automation and customization and shows you how to enter macro code and execute it.
The following code will zoom every page in the active document so that it fits in the drawing window. When it is done, it returns you to the page where you started. It happens so fast, you hardly notice that it was even working!
Option Explicit Const MESSAGE_CAPTION$ = "Fit All Pages" Public Sub SetAllPagesToFit() '// Get the active window, if it is a drawing window: Dim win As Visio.Window Set win = m_uiGetActiveDrawingWindow() If (win Is Nothing) Then Exit Sub Dim doc As Visio.Document Dim pg As Visio.Page, pgOrig As Visio.Page Set pgOrig = win.Page Set doc = pgOrig.Document '// Fit each window. -1 is the zoom value for '// fit-to-page: For Each pg In doc.Pages win.Page = pg win.Zoom = -1 Next '// Return the window to the original page: win.Page = pgOrig End Sub Private Function m_uiGetActiveDrawingWindow() As Visio.Window '// Checks the active window to see if it is '// a drawing window. If not, an error is presented '// to the user. Set m_uiGetActiveDrawingWindow = Nothing Dim win As Visio.Window If (Visio.ActiveWindow Is Nothing) Then Call MsgBox("This code requires an active drawing " & _ "window to function properly!", , _ MESSAGE_CAPTION$) Exit Function End If Set win = Visio.ActiveWindow If (win.Type <> Visio.VisWinTypes.visDrawing) Then Call MsgBox("The active window is not a drawing " & _ "window. Please make sure the active " & _ "window is a drawing window!", _<br /> , MESSAGE_CAPTION$) Exit Function End If Set m_uiGetActiveDrawingWindow = win End Function