legendaryspartan98
legendaryspartan98

Reputation: 61

Changing the Color of a Visio Shape via Powershell Script

I'm trying to automate the creation of network diagrams via Powershell and Visio but, I'm at a roadblock. I want to change the color of the Cloud (built-in Visio shape) but for some reason, the color does not change. Can you please help me solve this issue? (See code below)

Import-Module VisioBot3000 -Force

# Variables
$Username = $env:USERNAME
#set_x = 1
#set_y = 1
#Servers = Read-Host -Prompt "How many routers do you have?

# Opens the Visio application
New-VisioApplication
# Opens the Visio Document
Open-VisioDocument

# Register the network shapes stencil
$PERouterStencil = Register-VisioStencil -Name NetworkSymbols -Path 'C:\Program Files\...\NETSYM_U.vssx'
$VisioStencil2 = Register-VisioStencil -Name NetworkLocations -Path 'C:\Program Files\...\NETLOC_U.vssx'

#Register the router shape
$PE_Router_Shape = Register-VisioShape -Name Router -StencilName Network Symbols -MasterName Router
$Cloud_Register = Register-VisioShape -Name Cloud -StencilName NetworkLocations -MasterName Cloud

#Creating the Cloud
$Cloud = New-VisioShape Cloud -Label Cloud -x 5.6443 -y 4.481
#Width of the Cloud
$Cloud_Width = $Cloud.CellsU('Width').FormulaU = '3 in'
$Cloud_Height = $Cloud.CellsU('Height').FormulaU =  '1.89 in'
$Cloud.CellsU('FillForegnd').FormulaForceU = '=RGB(255,0,0)'
$Cloud.CellsU('FillBkgnd').FormulaForceU= 'THEMEGUARD(SHADE(FillForegnd,LUMDIFF(THEME("FillColor"),THEME("FillColor2"))))'

Upvotes: 0

Views: 403

Answers (1)

Mike Shepard
Mike Shepard

Reputation: 18156

Here's a function which will set the color. Visio shapes are complicated, involving sub-shapes and gradient shading. This function sets all of the shapes (and subshapes) to the color.

<#
.SYNOPSIS
    Sets the color of a shape
.DESCRIPTION
    Sets the color of a shape and subshapes to a given color
.EXAMPLE
    $shape= add-visioshape -stencil BasicShapes -name Square
    Set-ShapeColor -shape $shape -color Red
.INPUTS
    You cannot pipe any objects into Set-ShapeColor
.OUTPUTS
    None
.PARAMETER Shape
    The shape to apply the color to
.PARAMETER Color
    The color you want the shape to be
    #>
function Set-VisioShapeColor {
    [CmdletBinding()]
    Param($Shape,
        [System.Drawing.Color]$Color,
        [ValidateSet('FillForegnd', 'FillBkgnd')]$FillType = 'FillBkgnd'
        )
    $ColorFormula = "=THEMEGUARD(rgb($($Color.R),$($Color.G),$($Color.B)))"


    $shape.CellsU($fillType).FormulaForce = $ColorFormula
    $shape.CellsU('FillGradientEnabled').FormulaForce = 'FALSE'
    $shape.CellsU('FillPattern').FormulaForce = '1'
    $shape.shapes | foreach-object {
        $_.CellsU('FillGradientEnabled').FormulaForce = 'FALSE'
        $_.CellsU($fillType).FormulaForce = $colorFormula 
    }
}

Upvotes: 1

Related Questions