A while ago, I published a post on exporting Visio pages to PNG format. This is something I do quite often. This post received good amount of views and I had a couple of people reaching out to me asking if I can extend that function to export visio pages to other formats as well.
It turns out that it is a very easy thing to do. I took this opportunity to extend the function I created and added a few enhancements. Here is the updated function:
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 |
function Export-Visio { [CmdletBinding()] [OutputType([String])] Param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0 )] [ValidateScript({[System.IO.Path]::IsPathRooted($_)})] [string]$fileName, [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=1 )] [ValidateScript({[System.IO.Path]::IsPathRooted($_)})] [string]$destinationFolder, [Parameter(Mandatory=$false, ValueFromPipeline=$true, Position=2 )] [ValidateSet("png", "svg", "svgz", "vdw", "vss", "vst", "vdx", "vsx", "vtx", "gif", "jpeg")] [string]$To="png" ) Begin { try { $visio = New-Object -ComObject Visio.Application } catch { Write-Error $_ } } Process { $document = $visio.Documents.Open($fileName) $pages = $visio.ActiveDocument.Pages $pages | ForEach-Object { try { Write-Verbose "Exporting page $($_.Name) to ${To} format" $_.Export("${destinationFolder}\$($_.Name).${To}") } catch { Write-Error $_ } } } End { $document.Close() $visio.Quit() } } |
Now, this is how you use it:
1 |
Export-Visio -fileName "C:\hi-level-workflow.vsd" -destinationFolder C:\Images -To png -Verbose |
The -fileName and -destinationFolder parameter accept only absolute paths. The -To parameter has a validation set containing whatever I have tested. You can extend it easily if you know what other extensions can be used while exporting Visio pages to other formats. Just add the extensions to -To validation set. That is it.
Do let me know if you have any feedback on this and any other changes you want to see.