Search code examples
powershellvisio

Visio automation via PowerShell


I have a script that automates the a Visio diagram, i based my script on the official Office site: http://gallery.technet.microsoft.com/office/f77fb025-11ee-48f3-8409-9bb567a63fc3

Well to be honest i have no idea how to retrieve the values of the "shape data", this shape data is for example (Serial number, Building, Location, etc...) from the pc stencil. I want to add and revise this values programatically, i ve checked the object model reference but without luck.

Can someone help me?

here is the code, it opens a visio document and adds a pc stencil to the drawing.

$application = New-Object -ComObject Visio.Application
$application.visible = $true
$documents = $application.Documents
$document = $documents.Add("Basic Network Diagram.vst")
$pages = $application.ActiveDocument.Pages
$page = $pages.Item(1)


$ComputerStencil = $application.Documents.Add("Computers and Monitors.vss")



$pc = $ComputerStencil.Masters.Item("PC")
$shape1 = $page.Drop($pc, 2.2, 6.8)
$shape1.Text = "Some text...."

Thanxs for your time!!


Solution

  • The Visio Automation library may help you:

    Here's an example of how you can use it in C#. Using it in PowerShell should not be difficult.

    var app = new IVisio.Application();
    var doc = app.Documents.Add("");
    var page = doc.Pages[1];
    var shape1 = page.DrawRectangle(1, 1, 3, 4);
    VisioAutomation.CustomProperties.CustomPropertyHelper.Set(shape1,"Hello","World");
    var props = VisioAutomation.CustomProperties.CustomPropertyHelper.Get(shape1);
    

    Using the Visio PowerShell Module, the code will look something like this:

    Set-StrictMode -Version 2
    $ErrorActionPreference = "Stop"
    
    Import-Module Visio
    
    $app= New-VisioApplication
    $doc = New-VisioDocument
    $stencil_net = Open-VisioDocument "Basic Network Diagram.vst"
    $stencil_comp = Open-VisioDocument "Computers and Monitors.vss"
    
    
    $pc_master = Get-VisioMaster "PC" $stencil_comp 
    $shapes = New-VisioShape -Masters $pc_master -Points 2.2,6.8
    Set-VisioText "Some Text..."
    Set-VisioCustomProperty -Name "prop1" -Value "val1"
    Set-VisioCustomProperty -Name "prop2" -Value "val2"
    
    
    $shapedata_col = Get-VisioCustomProperty -Shapes $shapes 
    foreach ($shapedata in $shapedata_col)
    {
        Write-Host "--------------------------------------"
        Write-Host Name $shapedata.Name
        Write-Host Type $shapedata.Type
        Write-Host Value $shapedata.Value
        Write-Host Prompt $shapedata.Prompt
        Write-Host Ask $shapedata.Ask
        Write-Host Calendar $shapedata.Calendar
        Write-Host Format $shapedata.Format
        Write-Host Invisible $shapedata.Invisible
        Write-Host Label $shapedata.Label
        Write-Host LangId $shapedata.LangId
        Write-Host ShapeID $shapedata.ShapeID
        Write-Host SortKey $shapedata.SortKey
    }
    

    At the bottom you can see how to set and retrieve shape data.