Home > other >  How to Overwrite file on SaveAs without asking VB.NET
How to Overwrite file on SaveAs without asking VB.NET

Time:07-19

I am creating an exporter to export the data I make from some Queries to csv files.

My problem is that I am generating many csv, and my program asks me for each of the files to see if I want to overwrite the previous one.

Is there any way to overwrite them without having to ask me all the time?

Dim excel As Object
Dim workbook As Object
Dim sheet As Object

excel = CreateObject("Excel.Application")
workbook = excel.Workbooks.Open("C:\Users\me\Desktop\NECESARY.xlsx")

'I complete all I want to export here

workbook.SaveAs("C:\Users\me\Desktop\" & file & ".csv")
workbook.Close()

This works correctly, and exports all my data perfectly, but as told, I need not to be asked if I want to overwrite the file, there are too many files for going one by one accepting them.

CodePudding user response:

One solution is to delete the previous file before saving:

Dim excel As Object
Dim workbook As Object
Dim sheet As Object

excel = CreateObject("Excel.Application")
workbook = excel.Workbooks.Open("C:\Users\me\Desktop\NECESARY.xlsx")

'I complete all I want to export here

Dim fileName As String = "C:\Users\me\Desktop\" & file & ".csv"

System.IO.File.Delete(fileName)     

workbook.SaveAs(fileName)
workbook.Close()
  • Related