Search code examples
c#excelvisual-studioforms

Saving workbook, How to not deal with popups?


I am trying to update data in a excel file. I can open a workbook and add my new data. When I close the workbook it fires a thread that has a pop-up below all other pages asking if I want to save the pages. This leads to another pop-up asking the name of the new file.

What I need is to open the file, change data and close / update the file without human intervention.

Here is my current code:

public static int Make_new_row(string part_number, string part_name,
        string control_id, string serial_number, string test_operator)
{
    string file_name = "K:\\Test_Data\\Log_File\\Test_Log.xlsx";

    int rw = 0;
    int cl = 0;
    xlApp = new Excel.Application();


    int loop;
    for (loop = 0; loop < 10; loop++)
    {
        try
        {

            //Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(file_name);
            xlWorkBook = xlApp.Workbooks.Open(file_name, 0, true, 5, "", "",
                true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", 
                false, false, 0, true, 1, 0);
            break;
        }
        catch (Exception) // file is in use. wait for file avalibility
        {
            Thread.Sleep(1000);
            continue;
        }
    }

    if (loop == 10)
    {
        string message = "Unable to connect to file." + "\r" +
            "Troubleshoot issue and try again";
        string title = "Connection Error";
        MessageBox.Show(message, title);
        return -1;
    }

    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    range = xlWorkSheet.UsedRange;

    rw = range.Rows.Count;
    cl = range.Columns.Count;

    // make a new row
    xlWorkSheet.Rows[rw].Insert();

    // get last tracking # and generate a new one
    string last_traxcking_raw = Convert.ToString((xlWorkSheet.Cells[rw - 1,1]).Value);
    string last_traxcking = last_traxcking_raw.Remove(0,3);
    Data_File.last_test_file = last_traxcking;
    int t_number = Int32.Parse(last_traxcking);
    string new_tracking = ("TS-" + (t_number + 1));

    // populate new row with data
    xlWorkSheet.Cells[rw , 1] = new_tracking;
    xlWorkSheet.Cells[rw , 2] = part_number;
    xlWorkSheet.Cells[rw, 3] = part_name;
    xlWorkSheet.Cells[rw , 4] = control_id;
    xlWorkSheet.Cells[rw , 5] = serial_number;
    xlWorkSheet.Cells[rw , 6] = test_operator;


    //// quit and release
    xlWorkBook.Close();
    Marshal.ReleaseComObject(xlWorkBook);
    xlApp.Quit();
    Marshal.ReleaseComObject(xlApp);
    Marshal.ReleaseComObject(xlWorkSheet);
    Marshal.ReleaseComObject(range);
   


    return 1;
}

I am expecting to call this method and have my data updated leaving the file available for other instances of the program to interact with.


Solution

  • OK, The first issue was in variable 3 in the following statement:

    xlWorkBook = xlApp.Workbooks.Open(file_name, 0, false, 5, "", "",
    true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 
    0, true, 1, 0); 
                                                                                    
    

    Here it is false, originally it was true for "Open as read only"

    Secondly the following code fixed the issue:

    xlApp.DisplayAlerts = false;    
    xlWorkBook.SaveAs(file_name, AccessMode: XlSaveAsAccessMode.xlNoChange);
    xlWorkBook.Close();
    xlApp.DisplayAlerts = true;