0

This program is for a church food pantry, and I am doing it for free. My experience with C# is none, but I took the project because it sounds fun as I love to program. Anyway, my problem is I need to save the workbook before I close it, and I can't seem to figure out a way to do that.

The code and a few tries are listed here; the error I received is listed next to the try.

    private void btnExcelClose_Click(object sender, EventArgs e) 
    {
        exApp = new Microsoft.Office.Interop.Excel.Application();
        // this.Save(); CS1961 Form1 does not contain a defintion for 'Save' and no accesible extension method
        // thisworkbook.save(); CS0103 The name 'Thisworkbook' does not exist in the current contex
       // Excel.Workbook.save(); CS0117 Workbook does not contain a definition for 'save'
       // exApp.Workbooks.Save(); CS1061 'Workbooks' does not contain a defintion for 'Save' and no accesible extension method
        exApp.Workbooks.Close();
    }
PaulaJoann
  • 15
  • 1
  • 7
  • 3
    https://stackoverflow.com/search?q=%5Bexcel%5D%5Bvba%5D+save+workbook and https://stackoverflow.com/search?q=%5Bexcel%5D%5Bvba%5D+saveas Doesn't look to me like you've *exhausted google*. – Ken White Mar 23 '21 at 12:03
  • try this maybe ? - https://stackoverflow.com/questions/7012705/how-to-save-workbook-without-showing-save-dialog-with-excel-interop – abhijat_saxena Mar 23 '21 at 12:07
  • Several tries weren't listed; I didn't think of listing them till after. – PaulaJoann Mar 23 '21 at 12:10
  • The way they did the Save as won't work because Save is not a member of workbooks – PaulaJoann Mar 23 '21 at 13:08

1 Answers1

1

It is the Workbook class that implements the Save() and SaveAs() methods.

https://learn.microsoft.com/en-us/visualstudio/vsto/how-to-programmatically-save-workbooks?view=vs-2019

A few things to understand about C#: the keyword this has specific meaning to the class being used. And C# is case-sensitive.

Let's look at why your few tries have failed.

this.Save(); CS1961 Form1 does not contain a defintion for 'Save' and no accesible extension method

The above failed because this refers to your form class, not a Workbook object.

thisworkbook.save(); CS0103 The name 'Thisworkbook' does not exist in the current contex

Apparently you do not have a variable named thisworkbook.

Excel.Workbook.save(); CS0117 Workbook does not contain a definition for 'save'

The error is because you spelled save in lowercase, when it should be mixed Save. If you correct this, it will give you another error because Excel.Workbook does not refer to a specific Workbook. Maybe try ActiveWorkbook.

exApp.Workbooks.Save(); CS1061 'Workbooks' does not contain a defintion for 'Save' and no accesible extension method

Here you used the plural Workbooks which is a collection and it does not have a Save method. You must refer to a individual Workbook. Try exApp.Workbooks[0].Save().

Rick Davin
  • 1,010
  • 8
  • 10