How to open an Excel file in PrintPreview without suspending the code

132 views Asked by At

I'm new to C#/OpenXML and could not find an answer to this. Apologies in advance if it's a stupid question... Basically, I am writing an application that creates Excel files from an input string. This input string may contain information about multiple files that need to be created and opened in the print preview dialog simultaneously. However, using the following function, the code is suspended on the printpreview.show() method as it waits for the user to close the preview.

    public static void ExcelOpen(string fileName)
    {
        Excel.Application excelApp = new Excel.Application();
        excelApp.Visible = true;
        Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(fileName, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, false, 0, false, false);
        Excel.Worksheet ws = (Excel.Worksheet)excelWorkbook.Worksheets[1];
        excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrintPreview].Show();
        System.Runtime.InteropServices.Marshal.ReleaseComObject(excelApp);
        return;
    }

How can I avoid this and make sure the window stays opened in print preview but my program continues to run and create/display further files?

0

There are 0 answers