Skip to content

Instantly share code, notes, and snippets.

@ap0llo
Last active May 15, 2024 00:18
Show Gist options
  • Save ap0llo/1e24d9e66ef58f5de064c8aba2198b9e to your computer and use it in GitHub Desktop.
Save ap0llo/1e24d9e66ef58f5de064c8aba2198b9e to your computer and use it in GitHub Desktop.
Powershell script that exports Visio drawings in the current directory to png
# Powershell script that exports visio drawings to png
# Based on a F# script with the same purpose which can be found at
# http://stackoverflow.com/questions/1145269/visio-to-image-command-line-conversion
$outputFormat = ".png"
$inputFilePattern = "*.vsdx"
# Load Visio Interop Assembly
[Reflection.Assembly]::LoadWithPartialname("Microsoft.Office.Interop.Visio") > $null
# Visio Settings
$visOpenRO = 2
$visOpenMinimized = 16
$visOpenHidden = 64
$visOpenMacrosDisabled = 128
$visOpenNoWorkspace = 256
function Export-VisioDrawing($file)
{
$flags = [System.Convert]::ToInt16($visOpenRO + $visOpenMinimized + $visOpenHidden + $visOpenMacrosDisabled + $visOpenNoWorkspace)
$application = New-Object "Microsoft.Office.Interop.Visio.ApplicationClass"
$document = $application.Documents.OpenEx($file.FullName, $flags)
foreach($page in $document.Pages)
{
$fileNameWithoutExtension = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
# if document contains multiple pages, append page name to output file name
# otherwise use the same name as the input file
if($document.Pages.Count -gt 1)
{
$imagePath = Join-Path $file.Directory.FullName "$($fileNameWithoutExtension)_$($page.Name)$($outputFormat)"
}
else
{
$imagePath = Join-Path $file.Directory.FullName "$($fileNameWithoutExtension)$($outputFormat)"
}
$page.Export($imagePath)
}
$document.Close()
$application.Quit()
}
# Get visio files in the current directory and export all of them
$files = Get-ChildItem $inputFilePattern
foreach($file in $files)
{
Export-VisioDrawing $file
}
@IngmarPaetzold
Copy link

I wrote a similar solution as a VB macro inside a Visio document years ago. Problem is maintaining and copying these macros across documents, and VB as such... Thank you for this much, much better solution to do the job from outside of any document :-)

@ap0llo
Copy link
Author

ap0llo commented Feb 9, 2024

I wrote a similar solution as a VB macro inside a Visio document years ago. Problem is maintaining and copying these macros across documents, and VB as such... Thank you for this much, much better solution to do the job from outside of any document :-)

That's great to hear, glad it was useful to you.

@rg2609
Copy link

rg2609 commented Mar 13, 2024

Hi, @ap0llo
I am getting the following error while running the powershell script

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)
At C:\Users\aa\Downloads\Export-VisioDrawing.ps1:25 char:2
+     $document = $application.Documents.OpenEx($file.FullName, $flags)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

I am using the Visio 2019

@lyxal
Copy link

lyxal commented May 15, 2024

Worked a charm! Thanks for saving me a lot of work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment