通过PowerShell实现Visio自动化

问题描述 投票:2回答:2

我有一个自动化Visio图表的脚本,我的脚本位于官方Office网站上:http://gallery.technet.microsoft.com/office/f77fb025-11ee-48f3-8409-9bb567a63fc3

说实话,我不知道如何检索“形状数据”的值,这个形状数据例如是来自pc模板的(序列号,建筑物,位置等)。我想以编程方式添加和修改这些值,我检查了对象模型参考但没有运气。

有人能帮我吗?

这是代码,它打开一个visio文档并在图纸中添加一个pc模板。

$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...."

谢谢你的时间!!

powershell visio
2个回答
3
投票

Visio Automation library可以帮助您:

这是一个如何在C#中使用它的示例。在PowerShell中使用它应该不难。

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

使用Visio PowerShell Module,代码看起来像这样:

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
}

在底部,您可以看到如何设置和检索形状数据。


0
投票

您还可以将绘图导出为html,然后解析生成的data.xml。

© www.soinside.com 2019 - 2024. All rights reserved.