?
Solved

How do I resize a VISIO page from VBScript

Posted on 2011-09-16
2
Medium Priority
?
2,137 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Meetings to discuss business process can waste time, and often do .  The meeting's dialog can get confusing when participants have different professional perspectives and backgrounds.  A jointly-developed process picture helps wade through the confu…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

649 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