0

I have an issue when working with Excel from C# using the Microsoft.Office.Interop.Excel object.

The problem comes when I try and open a file to write to that I have already have open in Excel. This ultimately errors and then leaves an instance of Excel running in the background.

How can I tell that the file is already open before opening it?

Also what is the convention for disposing with the Excel objects and therefore killing the Excel process?

1

2 Answers 2

0

Hello you must to dispose your excel objects at end of treatment (workbook, applicationClass, usedRange, worksheet)

        workbook.Close(false, workbookPath, null);
        applicationClass.Quit();

        while (Marshal.ReleaseComObject(usedRange) > 0)
        { }
        while (Marshal.ReleaseComObject(worksheet) > 0)
        { }
        while (Marshal.ReleaseComObject(workbook) > 0)
        { }
        while (Marshal.ReleaseComObject(applicationClass) > 0)
        { }
Sign up to request clarification or add additional context in comments.

Comments

0

For opening of already opened - for more just type Workbooks.Open on the MSDN. Here is the save sample, you just overwrite (an still opened) without querying :o), xlShared and xlLocalSessionChanges are keywords.

            _xlsWorkbook.SaveAs(
                 targetFileName                                                     /* Filename */
                ,Excel_ForMissing.XlFileFormat.xlExcel8                             /* FileFormat */
                ,Missing.Value                                                      /* Password */
                ,Missing.Value                                                      /* WriteResPassword */
                ,Missing.Value                                                      /* ReadOnlyRecommended */
                ,Missing.Value                                                      /* CreateBackup */
                ,Excel_ForMissing.XlSaveAsAccessMode.xlShared                       /* AccessMode */
                ,Excel_ForMissing.XlSaveConflictResolution.xlLocalSessionChanges    /* ConflictResolution */
                ,false                                                       /* AddToMru */
                ,Missing.Value                                                       /* TextCodepage */
                ,Missing.Value                                                       /* TextVisualLayout */
                ,true                                                       /* Local */
            );

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.