• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Visio Guy

Smart graphics for visual people




  • Home
  • Hire Me
    • Hire Me
    • Résumé
  • Products
    • Products
    • Bubble Revision Shape
    • Layers to Pages Utility
    • Rack Unit Dimension Line
    • Radial Elements Tool with up to 100 Wedges
    • Text on a Circle Visio SmartShape
  • Index
    • Articles by Date
    • YouTube – VisioGuy
    • Download Information
    • Suggestion Box
    • Shop
    • Visio Art
    • Visio Links
    • – Visio Shapes & Stencils
    • – Visio Templates & Drawings
  • About
    • About
    • Donate
    • GitHub
    • jsFiddle
    • Reading List
    • Subscribe & Follow
      • – E-mail
      • – facebook
      • – Twitter
      • – RSS
    • Privacy Policy
  • Discussion Forum
You are here: Home / Development / Code / Fit All Pages to Window in a Visio Document

Fit All Pages to Window in a Visio Document

April 13, 2011 By Visio Guy 13 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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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!", _
                    , MESSAGE_CAPTION$) 
        Exit Function  
    End If
    Set m_uiGetActiveDrawingWindow = win
 
End Function
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!", _
                    , MESSAGE_CAPTION$) 
        Exit Function  
    End If
    Set m_uiGetActiveDrawingWindow = win
 
End Function
  • Tweet
  • More
  • Pocket
  • Share on Tumblr
  • Print
  • Email

Related posts:

  1. Combo Box Table of Contents
  2. Polygon Maker
  3. Polyline Shape Maker

Filed Under: Code Tagged With: Pages, Programming, Tools

Previous Post: « Visio SmartFace
Next Post: Visio Baseball Scorecard »

Reader Interactions

Comments

  1. Rob Fahrni says

    April 13, 2011 at 3:22 pm

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

  2. John Distai says

    April 15, 2011 at 2:32 pm

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

  3. Allan says

    May 2, 2011 at 8:07 pm

    how do you store code in a stencil?

  4. Allan says

    May 2, 2011 at 10:24 pm

    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$)

  5. Allan says

    May 2, 2011 at 10:34 pm

    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

  6. Jason says

    May 3, 2011 at 9:00 pm

    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

  7. Snowy says

    February 25, 2012 at 7:07 am

    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.

  8. Visio Guy says

    April 12, 2012 at 11:48 am

    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).”

  9. Robert says

    June 2, 2012 at 4:53 am

    Hi

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

    1
    2
    3
    4
    5
    6
    7
    8
    
    <br />
    Public Sub FitAllPagesToWindow()</p>
    <p>    Dim CurrPage As Visio.Page</p>
    <p>    For Each CurrPage In Application.ActiveDocument.Pages<br />
            Application.ActiveWindow.Page = CurrPage<br />
            Application.ActiveWindow.Zoom = -1<br />
        Next</p>
    <p>End Sub<br />
    <br />
    Public Sub FitAllPagesToWindow()</p>
    <p>    Dim CurrPage As Visio.Page</p>
    <p>    For Each CurrPage In Application.ActiveDocument.Pages<br />
            Application.ActiveWindow.Page = CurrPage<br />
            Application.ActiveWindow.Zoom = -1<br />
        Next</p>
    <p>End Sub<br />

    Regards
    Robert.

  10. Robert says

    June 2, 2012 at 5:08 am

    And to go back to the original page after resizing:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    
    <br />
    Public Sub FitAllPagesToWindow()</p>
    <p>    Dim OrigPage As Visio.Page<br />
        Dim CurrPage As Visio.Page</p>
    <p>    Set OrigPage = Application.ActiveWindow.Page</p>
    <p>    For Each CurrPage In Application.ActiveDocument.Pages<br />
            Application.ActiveWindow.Page = CurrPage<br />
            Application.ActiveWindow.Zoom = -1<br />
        Next</p>
    <p>    Application.ActiveWindow.Page = OrigPage<br />
        Application.ActiveWindow.Activate</p>
    <p>End Sub<br />
    <br />
    Public Sub FitAllPagesToWindow()</p>
    <p>    Dim OrigPage As Visio.Page<br />
        Dim CurrPage As Visio.Page</p>
    <p>    Set OrigPage = Application.ActiveWindow.Page</p>
    <p>    For Each CurrPage In Application.ActiveDocument.Pages<br />
            Application.ActiveWindow.Page = CurrPage<br />
            Application.ActiveWindow.Zoom = -1<br />
        Next</p>
    <p>    Application.ActiveWindow.Page = OrigPage<br />
        Application.ActiveWindow.Activate</p>
    <p>End Sub<br />

  11. Amar says

    April 21, 2015 at 3:17 pm

    Great Stuff Robert. Came really handy! I have 21 visio pages to toggle through and your code really handy.

    Inspires me to do my coding now.

    Thanks

  12. Terrance says

    November 26, 2015 at 4:54 am

    Hello Andrew,I was thinking of using the teaptlame you developed for one of my project, but the link is not working. Could you please email me the template.I really liked the site and the detail explanation you have given on each subject with the screen shot. I am going to use this site to build my knowledge.Thanks a lot,Sushe

Leave a Reply Cancel reply

Primary Sidebar

Buy Über Rack Unit Dimension Line
Article — Purchase

Categories

Buy my book!

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Tag Cloud

A/V Artistic Effects BPM Code Connectors Control Handles Countries Custom Patterns Custom Properties Data Graphics Data Linking Data Visualization David Edson David Parker Fill Format Formulas Functions Geometry Gradient Images Links Maps Multi-shapes Network Programming repeating shapes Resources Right-Click Actions Scale Shape Data ShapeSheet ShapeSheet Formulas ShapeSheet Functions SharePoint shiny SmartShapes Sport Sports Text Themes Tools Transparency User-defined Cells Visio 2007 Visio SmartShapes

Top Posts & Pages

  • - Visio Shapes & Stencils
  • - Visio Templates & Drawings
  • Amazon AWS Visio Shapes
  • Sankey Diagram Shapes for Visio
  • Free Visio People Shapes
  • Bubble Revision Shapes
  • Visio Network Server Shape Icon Customization Tool
  • AV Engineering Diagrams with Symbol Logic ECAV
  • Release the Power of Visio Custom Line Patterns
  • Crayon Visio Network Shapes, Revisited

www.visguy.com - Visio Guy - since 2006

 

Loading Comments...