ExportAsFixedFormat xlTypePDF does not work in the WindowsService1

gpns 21 Reputation points
2020-11-06T05:08:55.72+00:00

Hi!

I use VS Pro 2015
37877-01.png

The version of Microsoft Office 2010 and 2013 that you are using.
A WindowsService1 service project is being created in the C#.
The service uses code that opens any Excel *.xlsx file using the module Microsoft.Office.Interop.Excel.

37902-02.png

Next, select any sheets of the file and run the command to convert to PDF format.

ObjWorkSheet.ExportAsFixedFormat(
Excel.XlFixedFormatType.xlTypePDF,
localPath + localName + ".pdf",
Excel.XlFixedFormatQuality.xlQualityStandard, true, false,
Type.Missing, Type.Missing, false, Type.Missing);

This command is not executed, and a failure occurs that is not caught by the try block.

The service crashes the method and runs the method again. An open excel process does not terminate, and as a result, many Excel processes accumulate. In the folder there are more files with the extension *.tmp.

We believe that this is a problem of interaction between the service and the Excel module. If the same code is inserted into a regular Windows Forms application, it runs without errors.

Other programmers found similar errors on the Internet, but no one offered an answer or solution. Below links:

https://stackoverflow.com/questions/29009041/exportasfixedformat-not-working-with-excel2013

https://stackoverflow.com/questions/25252451/excel-2013-com-api-hangs-exportasfixedformat-under-service-account

Please help us resolve this issue.

Below is a code snippet:

Process appProcess = null;

            try  
            {  
                  
                Excel.Application ObjExcel = new Excel.Application();  
                appProcess = GetExcelProcess(ObjExcel);  
                Excel.Workbook ObjWorkBook = null;  
                Excel.Worksheet ObjWorkSheet = null;  
                Excel.Sheets ObjWorkSheets = null;  
                try  
                {  
                    ObjExcel.Visible = false;  
                    ObjExcel.ScreenUpdating = false;  

                    ObjExcel.EnableEvents = false;  
                    ObjExcel.DisplayStatusBar = false;  
                    ObjExcel.DisplayAlerts = false;  
                    ObjExcel.Workbooks.Add();  

                    ObjExcel.Calculation = Excel.XlCalculation.xlCalculationManual;  

                    ObjWorkBook = ObjExcel.Workbooks.Open(localPath + localName + ".xlsx");  

                    string[] sheetsToBeSelected = { configDateTime.Day.ToString(), "1", "2", "3" };  
                    ObjWorkSheets = (Excel.Sheets)ObjWorkBook.Sheets.Item[sheetsToBeSelected];  
                    ObjWorkSheets.Select();  
                    ObjWorkSheet = (Excel.Worksheet)ObjWorkBook.ActiveSheet;  
                   try  
                    {  
                        ObjWorkSheet.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, localPath + localName + ".pdf",  
                            Excel.XlFixedFormatQuality.xlQualityStandard, true, false, Type.Missing, Type.Missing, false, Type.Missing);  
                    }  
                    catch (Exception ex)  
                    {  
                        Program.log.Info("Ошибка" + ex.Message);  
                    }  
                }  
                catch (Exception ex)  
                {                          
                    Program.log.Info(string.Format("Проблема чтения Excel-файла, ошибка - {0}", ex.Message));  
                    appProcess.Kill();  
                }  
                finally  
                {                                          
                    Marshal.ReleaseComObject(ObjWorkSheet);  
                    ObjWorkBook.Close(false);  
                    Marshal.ReleaseComObject(ObjWorkBook);  
                    ObjExcel.Workbooks.Close();  
                    ObjExcel.Quit();  
                    Marshal.ReleaseComObject(ObjExcel);  
                    ObjExcel = null;  
                    ObjWorkBook = null;  
                    ObjWorkSheet = null;  
                    GC.Collect();  
                    GC.WaitForPendingFinalizers();  
                    appProcess.Kill();  

Program.log.Info("Good");
}
}
catch (Exception ex)
{
appProcess.Kill();
Program.log.Info(ex.Message);
}
appProcess.Kill();

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
39,567 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emily Hua-MSFT 27,651 Reputation points
    2020-11-06T09:00:44.303+00:00

    @gpns ,
    We are mainly responsible for general issues of Office desktop applications, but your question seems to be more related to code.
    Currently, Microsoft Q&A supports the products listed over here: supported topics (more to be added later on). Your question is not supported yet now.

    To better help you, I would suggest you ask the question on the forum below.

    Excel for Developers

    Thanks for your understanding.


    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.