When working in Visio, it is not uncommon that you need to export your diagram to a picture for sharing or placing in documentation. For example, when writing, I will often have multiple Visio diagrams that I continually tweak throughout the process. So, I wrote a function to take all the Visio diagrams in a folder and export them to SVG and PNG.
All you need to do is pass this function the path of the folder, and it will do the rest.
Function Export-VisioToImages {
<#
.SYNOPSIS
Use to export Visio diagrams to PNG and SVG format
.PARAMETER Path
Specifies the path to the folder with the Visio diagrams
.PARAMETER Filter
Specifies a filter to qualify the Path parameter. Default value is "*.vsdx"
.PARAMETER Force
Forces the command to overwrite existing export files.
.EXAMPLE
Export-VisioToImages -Path $Path -Force
.NOTES
Requires that Visio is installed on the local machine
#>
param(
[string]$Path,
[string]$Filter = "*.vsdx",
[switch]$Force
)
# Create the Visio object
$Visio = New-Object -comobject Visio.Application
$Visio.Visible = $false
# Get all the Visio files in the folder
$FilesToExport = Get-ChildItem $Path -Filter $Filter
foreach ($item in $FilesToExport) {
# Open the Visio document
$doc = $Visio.Documents.Open($item.FullName)
# Set the paths for the svg and png files
$ExportPaths = @(
Join-Path $item.DirectoryName "$($item.BaseName).svg"
Join-Path $item.DirectoryName "$($item.BaseName).png"
)
foreach ($export in $ExportPaths) {
if (Test-Path $export) {
# If file exists and force is true, the delete the existing file
if ($Force) {
Remove-Item $export -Force
}
# else if the file exists and force is not true, go to the next file
else {
Write-Warning "Skipping '$export' because it already exists. Use -force to overwrite it."
continue
}
}
# Export the Visio document
$doc.Pages | ForEach-Object {
$_.Export($export)
}
Write-Output $export
}
# Close the document
$doc.Close()
}
# Close Visio
$Visio.Quit()
}
The this post of part of the series Automation Authoring. Refer the main article for more details on use cases and additional content in the series.