Share:

" /> Visio Guy » Fit All Pages to Window in a Visio Document
Home » Code

Fit All Pages to Window in a Visio Document

Submitted by on April 13, 2011 – 9:14 am | | 20798 views 10 Comments

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

10 Comments »

  • Rob Fahrni says:

    I still love our good friend VBA. It’s still handy, and relevant, after all these years.

  • John Distai says:

    Thank you! I need this frequently, and this will make things so much simpler!

  • Allan says:

    how do you store code in a stencil?

  • Allan says:

    I try to run the macro and i get compile errors with

    If (win.Type <> Visio.VisWinTypes.visDrawing)

    Call MsgBox(“This code requires an active drawing ” & _
    “window to function properly!”, , _
    MESSAGE_CAPTION$)

    Call MsgBox(“The active window is not a drawing ” & _
    “window. Please make sure the active ” & _
    “window is a drawing window!”, _ , MESSAGE_CAPTION$)

  • Allan says:

    Works.the code is not formatted properly on the webpage. might have something to do with

    SyntaxHighlighter
    version 3.0.83 (July 02 2010)
    http://alexgorbatchev.com/SyntaxHighlighter

  • Jason says:

    I’ve made many attempts to figure out how to save this bit of code in a stencil (.vss). I have a personal stencil with lots of customized and purpose-built shapes that I’d like to have it in as this is my most-used stencil. I can’t seem to find the trick to make this work. I tried to save it to a new stencil, which works, but then the code is only available to a new page with that stencil, and only by opening VB — My reading of the above is that, once saved to a stencil, the code would be available to any document that is open when that stencil is open, but how to do this without opening VBA, etc.? The text above says ‘At the press of a button, …’

    Help?

    Thanks, Jason

  • Snowy says:

    Works great – once you get rid of the HTML bits in the code above and replace them with plain text. ie) &amp <&gt So it’s not just a straight copy and paste.

    As mentioned with the “win.Zoom = -1″ line, the -1 value sets it to fit-to-page (same as hitting the “Fit page to current window” icon in the bottom right corner. But by playing around with this value I stumbled across other possibilities (as I couldn’t find any definition of the impact of this value).

    Using 0.9 has the same effect as hitting the “-” Zoom Out button once (from a starting point of 100%), 0.8 is the same as hitting it twice etc etc. Using 1.1 has the same effect as hitting the “+” Zoom In button once. 1.2 twice, 1.3 three times etc.

    This allows you to display the pages at slightly lower or higher magnification if the fit-to-page magnification isn’t quite the best for your drawings. (I find there tends to be a large border area around the sheet which means the magnification is a bit less than it could be).

    One thing these decreased/increased magnifications don’t do though is centre the sheet, all they do is set the magnification. To auto-centre the sheets you need to first use the fit-to-page line, which also auto-centres, followed by the decreased/increased magnification value, ie)

    win.Zoom = -1
    win.Zoom = 1.2

    Then all the sheets will display properly centred and at whatever magnification you choose as your optimum.

    Hope that helps.

  • Visio Guy says:

    Thanks for the observations, Snowy!

    The Visio SDK help says this for the Window.Zoom property:

    “Valid values range from 0.05 to 9.99 (5% to 999%). The value -1 fits the page into the window. The default value is .67, which is equivalent to the Whole Page setting in the Zoom dialog box (on the View tab, in the Zoom group, click Zoom).”

  • Robert says:

    Hi

    The following might be simpler and does the same job (tested in Visio 2007, not sure if valid in all Visio versions):


    Public Sub FitAllPagesToWindow()

    Dim CurrPage As Visio.Page

    For Each CurrPage In Application.ActiveDocument.Pages
    Application.ActiveWindow.Page = CurrPage
    Application.ActiveWindow.Zoom = -1
    Next

    End Sub

    Regards
    Robert.

  • Robert says:

    And to go back to the original page after resizing:


    Public Sub FitAllPagesToWindow()

    Dim OrigPage As Visio.Page
    Dim CurrPage As Visio.Page

    Set OrigPage = Application.ActiveWindow.Page

    For Each CurrPage In Application.ActiveDocument.Pages
    Application.ActiveWindow.Page = CurrPage
    Application.ActiveWindow.Zoom = -1
    Next

    Application.ActiveWindow.Page = OrigPage
    Application.ActiveWindow.Activate

    End Sub

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.

*