简体   繁体   中英

How to Overwrite file on SaveAs without asking VB.NET

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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM