Andrii
Andrii

Reputation: 51

Visio programatically set page orientation with VBA

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.

Upvotes: 2

Views: 2093

Answers (3)

Matt Plank
Matt Plank

Reputation: 333

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;
    }
}

Upvotes: 1

SmrtGrunt
SmrtGrunt

Reputation: 919

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"

Upvotes: 3

John Visio MVP
John Visio MVP

Reputation: 11

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

John... Visio MVP

Upvotes: 1

Related Questions