Printing Multiple InfoPath forms (Automated)
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.
Write-Host
function Invoke-FormPrint
{
Param($InfoForm)
$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()"
$infopath.XDocuments[0].printout()
sleep 10
Write-verbose " – FormPrint: Calling Quit()"
$infopath.quit()
if(!$?)
{
Write-Host " - Error printing Form: $shortname" -fore RED
}
else
{
Write-Host " - Successfully printed Form: $shortname" -fore Green
}
}
$erroractionpreference = "silentlycontinue"
if($verbose){$verbosepreference="continue";$erroractionpreference="continue"}
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
Invoke-FormPrint $Form
}
John Negus :: Oct.30.2008 :: All :: 3 Comments »

