Infopath Inerop Assemblies Export to PDF RRS feed

  • Question

  • Hi There,

    I have this code for generating PDF from an infopath form. It works perfectly fine if I run it as a console application.


    app As Application = NewApplication()




    'Dim st As Object = CreateObject("InfoPath.Application")


    Dim xDoc As XDocument = app.XDocuments.Open(inputFile, XdDocumentVersionMode.xdIgnoreDataAdaptersQueryFailure)


    'Export to PDF




    'Wait for a second





    '_infopathMgr.ExportToPDF(inputFile, outputFile)


    Catch ex As Exception


    String.Format("Export failed with error:{0}{1}", ex.Message, ex.StackTrace))




    If app IsNot Nothing Then




    app =



    End If


    End Try

    If I use the above code in a Windows Service it hangs at the Open method doing nothing. The infopath form is a Full Trust form (with .NET code) and it's signed with a PKI certificate. The service is running under my account. Looks like there is a popup (security notice or something) causing the hang up. When I open the form manually I don't see any popups though.

    If I use a simple form with no .NET code, it works just fine. The PDF is generated without any issues.

    Did any of you run into this issue before? I'm hoping it's a office setting or something that I missed.

    Are there any alternative solutions like third party tools that I can start looking into for the PDF generation?



    Ramesh Tumati Sr. Information Systems Analyst

    Thursday, July 7, 2011 5:24 PM


  • Hi Ramesh,

    What is the version of your infopath application? How did you automate InfoPath via the Windows Service? I just crate a console application and use the following code snippet:

    Imports Microsoft.Office.Interop.InfoPath
    Module Module1
        Sub Main()
            Dim app As Application = New Application()
            Dim xDoc As XDocument = app.XDocuments.Open("C:\Users\peng\Desktop\form1.xml"XdDocumentVersionMode.xdIgnoreDataAdaptersQueryFailure)
                'Export to PDF  
                'Wait for a second  
            Catch ex As Exception
                ' MsgBox String.Format("Export failed with error:{0}{1}", ex.Message, ex.StackTrace))
                If app IsNot Nothing Then
                    app = Nothing
                End If
            End Try
        End Sub
    End Module

    It works well on my side. I think the problem must due to the windows service, I have to metioned that Microsoft does not support automation of Microsoft Office applications from any unattended, non-interactive client application or components(ASP, ASP.NET,NT Service),because Office may exhibit unstable behavior and/or deadlock when Office is run in the environment.For more details, please refer to this KB article:

    Please take a look at this thread:

    and one expert introduced the ,you can see whether it meet your requirement.

    Hope you can figure it out.

    Best Regards,


    Bruce Song [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Bruce Song Wednesday, July 20, 2011 8:24 AM
    Tuesday, July 12, 2011 2:56 AM