Posts RSS Comments RSS 253 Posts and 411 Comments till now

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.

Param($Path = "\\sharepoint.corp.com\Form_Folder",[switch]$verbose)

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
}

4 Responses to “Printing Multiple InfoPath forms (Automated)”

  1. on 19 Nov 2008 at 11:52 amDave

    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?

  2. on 20 Nov 2008 at 9:34 amJohn Negus

    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

  3. on 20 Nov 2008 at 10:15 amJohn Negus

    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.

  4. on 16 Apr 2014 at 5:42 ammsr

    I have tried running this script (with Infopath forms on a local drive). It opens each form, waits, then closes the form, but does not print. No error is given. It returns the message that the form was printed successfully. I’ve tried with the default printer as a regular printer (hard copy) and with my PDF print driver, but it appears that the printout() function is not working. I’m using Windows 7 Professional and Windows Powershell ISE (can’t quite tell what version, but maybe 1.0).

    Strange that the opening of Infopath works, but the print function doesn’t work.
    Thanks!

Trackback this post | Feed on Comments to this post

Leave a Reply

You must be logged in to post a comment.