Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Save/Overwrite existing Excel file without message

I need to export excel from viewlist, I used this code

Excel.Application app = new Excel.Application();
            //app.Visible = true;
            Excel.Workbook wb = app.Workbooks.Add(1);
            Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

            int i = 1;
            int i2 = 1;
            foreach (ListViewItem lvi in lvLogs.Items)
            {
                i = 1;
                foreach (ListViewItem.ListViewSubItem lvs in lvi.SubItems)
                {
                    ws.Cells[i2, i] = lvs.Text;
                    i++;
                }
                i2++;
            }


            wb.SaveAs(@"C:\1\myExcel.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
                        Type.Missing, Type.Missing);
            wb.Close(false, Type.Missing, Type.Missing);

            app.Quit();

        }

Now I need to overwrite the excel file without any message and I need to do this action every 10 min.

like image 529
didodido Avatar asked Aug 06 '14 07:08

didodido


People also ask

Does save as overwrite?

Save As -> Replace File If you are accustomed to selecting "File -> Save As" when saving documents, you can also overwrite the file with your changes this way. Select "Replace File. This is the same behavior as File Save." The original file will be overwritten.

What does overwrite changes in Excel?

Overwriting is the rewriting or replacing of files and other data in a computer system or database with new data. … Saving the new one will overwrite the previous file, even if that save is as innocuous as changing the title or retaining it.

How do I overwrite a file in VBA?

Run the above macro twice from a macro-enabled workbook. The second time that you run it, you will see a confirmation dialogue box asking if you want to overwrite the existing file or not. Now, the file will overwrite the existing file without making a mention of it.


1 Answers

try this one.

       app.DisplayAlerts = false;         wb.SaveAs(@"C:\1\myExcel.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,                     Type.Missing, Type.Missing);         workbook.Close();         app.Quit();         
like image 198
USER87 Avatar answered Oct 06 '22 14:10

USER87