none
Using Microsoft.Office.Interop.Access on Windows Service to export reports RRS feed

  • Question

  • I have a Windows Service application that will export MSAccess reports as PDF to serve a web application.

    But when I open the report, the process freezes and nothing happens. 

    I don't know wat is missing on my code, or what I'm doing wrong.

    On a Windows Forms Application, this method works normally and the export happens.

    The code is:

    using Microsoft.Office.Interop.Access;
    
    // ...
    
    public void ExportReports(List<string> reports, string destinationPath, string reportIdentifierPrefix)
            {
                var app = new Microsoft.Office.Interop.Access.Application();
    
                app.OpenCurrentDatabase(this.ReportingToolPath);
    
                app.Visible = false;
    
    
                foreach (var report in reports)
                {
                    app.DoCmd.OpenReport(report, AcView.acViewPreview, Type.Missing, Type.Missing, AcWindowMode.acHidden, Type.Missing);
    
                    app.DoCmd.OutputTo(AcOutputObjectType.acOutputReport, report, "PDF Format (*.pdf)", Path.Combine(destinationPath, $"{reportIdentifierPrefix}_{report}.pdf"), false, null, null, AcExportQuality.acExportQualityPrint);
                }
    
                app.CloseCurrentDatabase();
            }


    Monday, October 2, 2017 1:01 PM

Answers

All replies

  • Because of limitations in the threading model, running any of the Microsoft Office apps under a Windows Service would not be supported.

    Considerations for server-side Automation of Office


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, October 2, 2017 1:05 PM
  • In this case, the best solution for this issue is convert the windows service application into a console application running on the current user?
    Monday, October 2, 2017 1:14 PM
  • In this case, the best solution for this issue is convert the windows service application into a console application running on the current user?

    That would probably be one option. As long as Access is running in the user interactive session you shouldn't have any potential threading issues. That is, unless your C# Windows Forms app is attempting to create multiple threads to the same Access object instance.

    Just make sure to properly destroy your Access objects. The below article is for Excel but would also apply to Access:

    https://www.add-in-express.com/creating-addins-blog/2013/11/05/release-excel-com-objects/


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Monday, October 2, 2017 1:43 PM