How do I resize a VISIO page from VBScript

Posted on 2011-09-16
Last Modified: 2012-05-12
Hi Experts,

I'm trying to resize my VISIO Page using the following VBScript code, but page size remains unchanged:

'VBScript for VISIO - Set up a new doc, then resize the page
Dim MyApp, MyTemplate, MyPage, UndoScopeID1
'Set up a new doc
Set MyApp = CreateObject("Visio.Application")
Set MyTemplate = MyApp.Documents.Add("Basic Flowchart.vst")
Set MyPage = MyTemplate.Pages.Item(1)
'Resize the page
UndoScopeID1 = MyApp.BeginUndoScope("Lay Out Shapes")
MyPage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU ="680 mm"
MyPage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageDrawSizeType).FormulaU = "3"
MyPage.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOResizePage).FormulaForceU = "TRUE"
MyPage.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOAvenueSizeX).FormulaForceU = "9.25 mm"
MyApp.EndUndoScope UndoScopeID1, True

The last few lines of code are derived from a macro recorded in VISIO.  The macro itself works OK:

Dim UndoScopeID1 As Long
  UndoScopeID1 = Application.BeginUndoScope("Lay Out Shapes")
  Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = "640.825 mm"
  Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageDrawSizeType).FormulaU = "3"
  Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOResizePage).FormulaForceU = "TRUE"
  Application.ActiveWindow.Page.PageSheet.CellsSRC(visSectionObject, visRowPageLayout, visPLOAvenueSizeX).FormulaForceU = "9.525 mm"
  Application.EndUndoScope UndoScopeID1, True

Please help.

Question by:AnwaPeck
LVL 11

Accepted Solution

Visio_Guy earned 500 total points
ID: 36548417
Hi AP,

Visio's recorder-generated macros are hard to re-use without lots of cleanup. Here's a cleaner sample of resizing pages in Visio. Keep in mind that there are several ways to get a page object (such as doc.Pages(3) instead of Visio.ActivePage).

Also, if you are setting your page size to a standard-size piece of paper, then the settings might be different than what I've shown here. The code snippet below is for an arbitrary sized drawing page.

If you switch on Developer mode, you can right-click a page and choose Show ShapeSheet. There you can see which cells change when you change the size of a page via Page Setup, and modify the code below, accordingly.

Hopefully this snippet is clearer than the recorder macro above, and will show you the essentials of what you need to know.

Sub ResizePage()

  '// Get a Visio page object. There are several ways to
  '// do this, but we'll just start with the active page:
  Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  '// Get the ShapeSheet for the page:
  Dim shpPg As Visio.Shape
  Set shpPg = pg.PageSheet
  '// Set cells in the ShapeSheet to control the page size:
  shpPg.CellsU("PageWidth").ResultIU = 12 '...Note: IU means inches!
  shpPg.CellsU("PageHeight").ResultIU = 3
  '// To set the page size in millimeters, you would do this:
  'shpPg.CellsU("PageWidth").Result(Visio.VisUnitCodes.visMillimeters) = 600
  'shpPg.CellsU("PageHeight").Result(Visio.VisUnitCodes.visMillimeters) = 600
  '// If the page size is irregular, you should set the size
  '// type to 'custom'.
  shpPg.CellsU("DrawingSizeType").ResultIU = 3 '...visCustom
  '// Turn off auto-sizing pages:
  '// Visio 2010 only!
  If (pg.Application.Version >= 14) Then
    shpPg.CellsU("DrawingResizeType").ResultIU = 2 '...visAutoSizeOff
  End If
End Sub

Open in new window


Author Closing Comment

ID: 36548849

Excellent.  And the clear guidelines are much appreciated.

My new working VBScript. (I could not find a way to use the vis* properties in my VBScript to resize in millimeters, but I can get by with inches for now):

'VBScript for VISIO
Dim MyApp, MyTemplate, MyPage, MyShpPage
'Create new Visio Page Object
Set MyApp = CreateObject("Visio.Application")
Set MyTemplate = MyApp.Documents.Add("Basic Flowchart.vst")
Set MyPage = MyApp.ActivePage
Set MyShpPage = MyPage.PageSheet
'Resize the page (and set size type to CUSTOM)
MyShpPage.CellsU("PageWidth").ResultIU = 24
MyShpPage.CellsU("PageHeight").ResultIU = 24
MyShpPage.CellsU("DrawingSizeType").ResultIU = 3

Thank You.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now