• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2204
  • Last Modified:

How do I resize a VISIO page from VBScript

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"
MyPage.Layout
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.ActiveWindow.Page.Layout
  Application.EndUndoScope UndoScopeID1, True

Please help.

0
AnwaPeck
Asked:
AnwaPeck
1 Solution
 
Visio_GuyCommented:
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

0
 
AnwaPeckAuthor Commented:
Visio_Guy!!!!

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.
0

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now