Solved

How do I resize a VISIO page from VBScript

Posted on 2011-09-16
2
1,970 Views
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"
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
Comment
Question by:AnwaPeck
2 Comments
 
LVL 11

Accepted Solution

by:
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

0
 

Author Closing Comment

by:AnwaPeck
ID: 36548849
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

730 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