7

I am converting excel file to pdf using interop. and i have a working code.

but before saving it to pdf. it prompt a dialog box which ask the user to "save changes to the file or not" how can i avoid this prompt?

and how can i close the excel when saving is done? Thank you

public string ExceltoPdf(string excelLocation, string outputLocation)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelLocation);
                wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, outputLocation);

                wkb.Close();
                app.Quit();

                return outputLocation;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                throw ex;
            }
        }

2 Answers 2

3

Try adding

app.DisplayAlerts = False

after you set .Visible.

Sign up to request clarification or add additional context in comments.

Comments

1

Open your Excel as ReadOnly

Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open(excelLocation, ReadOnly: true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.