0

I wrote up a method that takes all the cells from a DataGridView and copies them to an Excel workbook.

It all works fine and dandy, up until I get to the part where I actually save it.

Here is the code that saves it:

        //Saves the Workbook to the specified path
        excelWorkbook.ActiveWorkbook.SaveCopyAs(pathToSave);
        excelWorkbook.ActiveWorkbook.Saved = true;

        //Close the workbook
        excelWorkbook.Quit();

And the exception being thrown:

Microsoft Office Excel cannot access the file 'C:\Users\TheGateKeeper\Desktop\New folder'. There are several possible reasons:

• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.

This is the first time I am working with this class, so I may have messed something up.

Also as a sidenote, what does:

 excelWorkbook.ActiveWorkbook.Saved = true;

do?

Thanks.

3
  • 'C:\Users\TheGateKeeper\Desktop\New folder' is directory. How you set excel filename? Commented Mar 6, 2012 at 18:24
  • Doh! To what extension does the interlop class save as? Commented Mar 6, 2012 at 18:28
  • 1. What are value of variable pathToSave? 2. Are directory ''C:\Users\TheGateKeeper\Desktop\New folder' exists? Commented Mar 6, 2012 at 18:45

1 Answer 1

1

Set the argument of the SaveCopyAs method to path of the excel file including extension.

eg. C:\Users\TheGateKeeper\Desktop\TestFile.xls.

The extension specifies the format of the workbook, eg .xls (2003 and <), .,xlsx (2007 +)

For your side note, setting Saved to true flags the content as saved it doesn't actually write it to disk.

To open a workbook file, use the Open method.

To mark a workbook as saved without writing it to a disk, set its Saved property to True.

The first time you save a workbook, use the SaveAs method to specify a name for the file.

Source MSDN

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

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.