title: Saving an excel workbook C#
body: <p>I'm busy with an application that manipulates excel data using an C# application.</p>
  
  <pre><code>xlApp = new Excel.Application();
              xlWorkbook = xlApp.Workbooks.Open(ConString, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
              xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets.get_Item(1);
  </code></pre>
  
  <p>Up until this point, I've saved the workbook by simply saying "xlApp.Quit" this would bring up the standard, "Would you like to save your changes" dialog you get from any Microsoft office product.</p>
  
  <p>However I was having an issue with the workbooks data that was being saved, so I was told to use the Workbook.Save method. It now saves the correct data but I cannot specify the filename/location. </p>
  
  <p>I read about the .SaveCopyAs method, I'm using a save file dialog to provide the filename/location, but when I push save and close the application there is no file to be found.</p>
  
  <p>This is the Savefile method.</p>
  
  <pre><code>    private void SaveFile()
      {
          using (SaveFileDialog Save = new SaveFileDialog())
          {
              Save.Filter = "Excel Files (*.xlsx)|*.xlsx";
              Save.InitialDirectory = "C:";
              Save.Title = "Save";
              if (Save.ShowDialog() == DialogResult.OK)
              {
                  // Using .SaveCopyAs doesnt save at all
                  xlWorkbook.SaveCopyAs(saveFileDialog1.FileName);
                  xlWorkbook.Saved = true;
                  //Using .Save saves the file, but i cannot specify the location/File name
                  //xlWorkbook.Save();
                  xlApp.Quit();
                  ReleaseObject(xlWorksheet);
                  ReleaseObject(xlWorkbook);
                  ReleaseObject(xlApp);
              }
          }
      } 
  </code></pre>
  
  <p>Not sure if this is relevant but here is my ReleaseObject method.</p>
  
  <pre><code>private void ReleaseObject(object obj)
          {
              try
              {
                  System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                  obj = null;
              }
              catch(Exception ex)
              {
                  obj = null;
                  MessageBox.Show("Unable to release the object " + ex.ToString());
              }
              finally
              {
                  GC.Collect();
              }
          }
  </code></pre>
  
  <p>I've seen alot of people saying their SaveCopyAs method isn't saving either and that's been due to them not giving the file a name/extension. This was the first thing I checked. This interop stuff is new to me, so please forgive any errors.</p>
  
  <p><a href="https://i.stack.imgur.com/MdXwR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MdXwR.png" alt="Filename variable"></a></p>
  
  <p>Thank you</p>