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 »


I’d love to use this code to print multiple infopath forms but I don’t know anything about powershell. Can you help me get started?
Sure……
1. First you need to install PowerShell on your computer running InfoPath.
2. Once it is installed, launch PowerShell and at the prompt type: Set-ExecutionPolicy RemoteSigned
3. Copy the script to Notepad.exe and save it as “PrintForms.ps1″
4. At the PowerShell command prompt goto the folder where you saved the script
5. Type: .\PrintForms.ps1 “\\sharepoint.corp.com\Form_Folder”
Note: In the example above I am specifying the UNC path to a SharePoint folder that contains all the forms that I want to print. You can enter a local path if that is where your forms are stored.
The script will perform a DIR of the specified folder for all the XML forms. For each form located in the folder it will open InfoPath 2003, print the form, wait 10 seconds, close InfoPath and then start all over again for the next form until all are printed.
I hope this helps, John
PS…..
I just noticed that when the script was uploaded there was a formatting error. The line:
“$Forms = dir $path *.xml+site:msdn.microsoft.com”>xml” should actually read:
“$Forms = dir $path *.xml”. I am also getting Brandon to upload the script so that you can download it.
The last thing. If PowerShell has problems connecting to the remote path then connect to it using explorer first, leave the explorer window open and try running the script again.