2

I need to change Page Orientation property to the "Landscape" value in a programmable way using VBA. Currently, I'm using the following code:

Application.ActivePage.PageSheet.CellsU("PrintPageOrientation").Formula = 2

But this only works for a printer not for the page I'm changing.

I've been looking for appropriate Cell in the Microsoft documentation but I haven't had any success.

Thanks for any help.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrii
  • 31
  • 6

3 Answers3

2

I know this is an old question, but for anybody else looking for an answer: Changing the PrintPageOrientation cell in Visio does change the orientation of the page in the document, not just for printing.

For a page object with the variable name vzpVisioPage, the following line will set landscape orientation:

vzpVisioPage.PageSheet.CellsSRC(visSectionObject, visRowPrintProperties, _
    visPrintPropertiesPageOrientation).FormulaForceU = "2"

To make a legal-sized page, use these lines:

vzpVisioPage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageWidth).FormulaU = "14 in"
vzpVisioPage.PageSheet.CellsSRC(visSectionObject, visRowPage, visPageHeight).FormulaU = "16.5 in"
SmrtGrunt
  • 869
  • 12
  • 25
  • This was exactly right, thank you for posting! Just a quick note that it will only change the attribute, you need to manually set the new height and width yourself. – Matt Plank Aug 29 '19 at 23:01
1

Based off of SmrtGrunt's solution above, I created a C# example:

///above in includes
using Visio = Microsoft.Office.Interop.Visio;

///above in private fields
private Visio.Page ActivePage;

///above in constructor
ActivePage = Globals.ThisAddIn.Application.ActivePage;

/// <summary>
/// Sets the currently open document to landscape (if not already set)
/// </summary>
public void SetOrientationToLandscape()
{
    var orientation = ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
        (short) Visio.VisRowIndices.visRowPrintProperties,
        (short) Visio.VisCellIndices.visPrintPropertiesPageOrientation].FormulaU;

    //If orientation flag isn't set to 'landscape'
    if (orientation != ((int)Visio.tagVisCellVals.visPPOLandscape).ToString())
    {
        //Landscape
        ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
            (short) Visio.VisRowIndices.visRowPrintProperties,
            (short) Visio.VisCellIndices.visPrintPropertiesPageOrientation].FormulaU = ((int)Visio.tagVisCellVals.visPPOLandscape).ToString();

        var currentWidth = ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
            (short) Visio.VisRowIndices.visRowPage, (short) Visio.VisCellIndices.visPageWidth].FormulaU;
        var currentHeight = ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
            (short) Visio.VisRowIndices.visRowPage, (short) Visio.VisCellIndices.visPageHeight].FormulaU;


        ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
            (short) Visio.VisRowIndices.visRowPage, (short) Visio.VisCellIndices.visPageWidth].FormulaU = currentHeight;
        ActivePage.PageSheet.CellsSRC[(short) Visio.VisSectionIndices.visSectionObject,
            (short) Visio.VisRowIndices.visRowPage, (short) Visio.VisCellIndices.visPageHeight].FormulaU = currentWidth;
    }
}
Matt Plank
  • 333
  • 6
  • 12
1

Just set the height and width of the page. There is no cell for landscape / portrait.

John... Visio MVP