Open Excel File, Refresh Query and Save C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart1044
    New Member
    • Oct 2012
    • 1

    Open Excel File, Refresh Query and Save C#

    Hi

    I am trying to open an Excel file, refresh the query behind it and then save and close the file in C#.

    I have the following but I am getting errors on the
    Code:
     Application excel = new Application()
    section:

    Code:
                Application excel = new Application();
                Workbook theWorkbook = excelFile.Workbooks._Open(txtLocation.Text, 0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false, System.Reflection.Missing.Value, false);
                Sheets sheets = (Sheets)theWorkbook.Worksheets;
                theWorkbook.RefreshAll();
                theWorkbook.Save();
                excel.Quit();
    I have added the Microsoft.Offic e.Interop.Excel reference, but still have the issue?
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It would help to know what the error message is. But I'm guessing your problem is that you didn't specify that you're creating an excel application. Just calling the variable excel doesn't make it an excel application, you can name the variable anything you want.

    Comment

    Working...