My name is John Negus and I work for a small consulting company called MSEtechnology. I specialize in Active Directory and Microsoft Operating Systems, with a little bit of Exchange and everything else Microsoft thrown in. I have taught a lot of internal AD courses for Microsoft all over the world and my current contract has me working on site alongside Brandon “The Power” Shell. That is where I was presented with the challenge of automating the printing of multiple InfoPath 2003 forms.
I was told, by Microsoft, that this was not an easy thing to do and that I could open up an advisory case in which they could give me some sample code to do this. I was also informed that I could create a transform file for Word and print all the forms using Word. I attempted this approach but gave up as it was long and drawn out.
Alas, PowerShell came to the rescue. Once I found the correct COM object to use and with the help of the “Great One” (that’s Brandon, in-case you did not know) it was easy, as you can see below.
$shortname = $InfoForm.name
Write-Verbose " – FormPrint: Creating COM Object"
$infopath = New-Object -com InfoPath.Application -verbose:$false
Write-Verbose " – FormPrint: Calling Open(`$InfoForm.fullname,10)"
$infopath.XDocuments.Open($InfoForm.fullname,10) | out-null
Write-verbose " – FormPrint: Calling PrintOut()"
Write-verbose " – FormPrint: Calling Quit()"
Write-Host " – Error printing Form: $shortname" -fore RED
Write-Host " – Successfully printed Form: $shortname" -fore Green
$erroractionpreference = "silentlycontinue"
Write-Verbose " + Processing all Forms from: $path"
$Forms = dir $path "*.xml"
Write-Verbose " + $($Forms.count) Forms found"
Write-host " + Processing all $($Forms.count) Forms located at: $path" -fore White
foreach($Form in $Forms)
Write-Host " + Printing Form: $($Form.Name)" -fore White