Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading