VisioPage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
Function New-VisioPage{
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param([string]$Name,
    $Visio=$script:Visio)

    if($PSCmdlet.ShouldProcess('Creating a new Visio Page')){
        $page=$Visio.ActiveDocument.Pages.Add( )
        if($Name){
            $page.NameU=$Name 
        }
        $page
    }
}

<#
        .SYNOPSIS
        Change the active page in Visio

        .DESCRIPTION
        Changes the active page in Visio to the page named in the parameter

        .PARAMETER Name
        Page name in the Visio document which you want to switch to

        .PARAMETER Visio
        Optional reference to a Visio Application (used if writing to multiple diagrams at the same time?)

        .INPUTS
        None. You cannot pipe objects to Set-VisioPage

        .OUTPUTS
        None

        .EXAMPLE
        Set-VisioPage -Page 'Page-3'


#>

function Set-VisioPage{
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param([string]$Name,
    $Visio=$script:Visio)
    if($PSCmdlet.ShouldProcess('Switching to a different Visio Page','')){
        $page=get-VisioPage $Name
        $Visio.ActiveWindow.Page=$page 
    }
} 

<#
        .SYNOPSIS
        Returns a visio page

        .DESCRIPTION
        Returns either the named page or the active page if nothing was named.

        .PARAMETER Name
        The name of the page you want. If you don't supply a name, the active page will be output.

        .INPUTS
        None. You cannot pipe objects to Get-VisioPage.

        .OUTPUTS
        Visio.Page

        .EXAMPLE
        $activePage=get-VisioPage
        #Returns the active page

        .EXAMPLE
        get-VisioPage 'Page-3'
        #returns the page named 'Page-3'


#>

Function Get-VisioPage{
    [CmdletBinding()]
    Param($Name)
    if ($Name) {
        try {
            $Visio.ActiveDocument.Pages($Name) 
        } catch {
            write-warning "$Name not found"
        }
    } else {
        $Visio.ActivePage
    }
}


<#
        .SYNOPSIS
        Deletes a page from Visio

        .DESCRIPTION
        Deletes a named page or the active page if no page is named.

        .PARAMETER Name
        The name of the page to remove. If no page is named, the active page is removed.

        .PARAMETER Parameter2
        Describe Parameter1

        .INPUTS
        What can be piped in
        None. You cannot pipe objects to Remove-VisioPage

        .OUTPUTS
        None

        .EXAMPLE
        Remove-VisioPage 'Page-3'
        #removes page 3

        .EXAMPLE
        Remove-VisioPage
        #removes the active page

#>

Function Remove-VisioPage{
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param($Name)
    if($PSCmdlet.ShouldProcess('Removing page named <$Name> or current page','')){
        if ($Name) {
            $Visio.ActiveDocument.Pages($Name).Delete(0)
        } else {
            $Visio.ActivePage.Delete(0)
        }
    }

}


<#
        .SYNOPSIS
        Switches the page orientation

        .DESCRIPTION
        Set the page orientation to either Landscape or Portrait

        .PARAMETER Landscape
        Changes the page orientation to Landscape

        .PARAMETER Portrait
        Changes the page orientation to Portrait

        .INPUTS
        None. You cannot pipe objects to Set-VisioPageLayout

        .OUTPUTS
        None

        .EXAMPLE
        Set-VisioPageLayout -Portrait

        .EXAMPLE
        Set-VisioPageLayout -Landscape

#>

Function Set-VisioPageLayout{
    [CmdletBinding(SupportsShouldProcess=$True)]
    Param([switch]$Landscape,[switch]$Portrait)
    if($PSCmdlet.ShouldProcess('Visio','Switch page layout')){
        if($Landscape){
            $Visio.ActivePage.Shapes['ThePage'].CellsU('PrintPageOrientation')=2
        } else {
            $Visio.ActivePage.Shapes['ThePage'].CellsU('PrintPageOrientation')=1
        }
    }
}