Read my intro to Powershell yet?
Anyway, what I’m about to explain here assumes that you have a basic understanding of Powershell, so you should read a real and complete tutorial on the subject.
To control a software with Powershell, you must first put your hand on it. The easy way is to start it yourself and put the result in a variable, such as shown here:
$x = New-Object -com Excel.Application
Than you can make it visible:
$x.Visible = $true
… and change some value in an active cell:
$a.ActiveCell.Value2 = "x"
Basic Powershell 101 (which should blow your mind, if you never saw it before).
But how can you get a grip on an application that is already running? Say I have a Visio document and need to add automatically some little things in it. In this case, you can store the Visio application with the following:
$visio = [Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")
This was the only really tricky part. Everything else can be found in the Visio Automation Object Model Reference.
Obtaining the Visio application would be much easier for future uses with a function:
function get-visio {[Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")}
Now let’s try our shiny new methods. How can I publish a page to PDF from Powershell? With the ExportAsFixedFormat method. This method is applicable to a Visio’s document. How do we select one?
$visio_doc = $visio.activedocument
Or in a function:
function get-visiodoc {Param ($visio_instance) $visio_instance.activedocument}
This way, we can obtain the active document with the following:
$visio_doc = (get-visiodoc (get-visio))
And now the method itself:
$visio_doc.ExportAsFixedFormat(1,"test-publish.pdf",1,1,1,1,$false,$true,$false,$false,$true)
This will export a single page PDF named “test-publish.pdf”. Ok, not really useful in itself. What if we want to export every page from the document in as many PDF? This is becoming quite a clicking-heavy task for a menu user. Not with Powershell!
Start with a single page publish function:
function export-page-pdf {param ($visio_page, $visio_path = "") $filename = $visio_path + $visio_page.name + ".pdf" $page_to_export = $visio_page.index (get-visiodoc (get-visio)).exportasfixedformat(1,$filename,1,1,$page_to_export,$page_to_export,$false,$true,$false,$false,$true)}
Notice the optional argument $visio_path
.
And now a looping function to publish every single page of the document:
function export-document-pdf { $active_document = (get-visio).activedocument $document_path = $active_document.path $active_document.pages | foreach-object {export-page-pdf $_ $document_path }}
Tada! The export-document-pdf
function doesn’t require any argument, so simply calling it will result in every page being published in PDF! You can even save the function and pin it to your taskbar. Next time you want to export a document, “click”, done!
With this example and the Visio Automation Object Model Reference, you should be able to do great things! Godspeed!