Search code examples
.netvb.netwinformsopenfiledialog

Creating and opening a new excel file using open file dialog


I have a open file dialog from where i will open excel files and download data into them. But in the open file dialog if I give a new file name and click on the OK button, it says File not found. Instead I want to create a file if the given file name does not exist and continue with my work. How can I achieve this? Thanks in advance.


Solution

  • By default, the OpenfileDialog control checks to make sure that the file exists. To stop this happening, before you show the dialog, or when your calling form is loaded, add this line .. changing "OpenFileDialog1" to whatever yours is called.

    OpenFileDialog1.CheckFileExists = False
    

    Then, when you press ok, the filename you typed is returned to your application. Of course, you'll need to check if the file exists yourself and decide what to do.