Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hello I have a program that imports an excel file using the oledb in vb.net. This program imports the excel spreadsheet into a datagridview. On opening the program will ask the user to choose an excel file to open, however if the user inputs nothing or presses the cancel button the program will crash. I'm trying to find a way to prevent a user from canceling or leaving the excel filename blank. I'm hoping this will be done using a try catch block but I'm not exactly familiar with try catch in vb.net. If anyone has any suggestions or solutions for this I would greatly appreciate it. This is what I found on MSDN.


VB
Dim dlg As New OpenFileDialog()
        dlg.Filter = "Excel Macro Enabled Files|*.xlsm*|Excel Files|*.xls|Excel 2007 Files|*.xlsx|All Files|*.*"
        If dlg.ShowDialog() = DialogResult.OK Then
            '      excelObj = New ExcelObject(dlg.FileName)
            txtFilePath.Text = dlg.FileName

            Me.ExcelWriteBtn.Enabled = txtFilePath.Text.Length > 0
            Me.closexlsbtn.Enabled = txtFilePath.Text.Length > 0
        Else
            messagebox.show("Please Select a File")
            dlg.showdialog()
        End If
Posted
Updated 14-Aug-13 8:07am
v3
Comments
ZurdoDev 14-Aug-13 11:15am    
Your solution of looking for the file first is better than using a try catch. What won't work about that approach?
Sergey Alexandrovich Kryukov 14-Aug-13 11:53am    
It's not about try-catch. If you look at the code, you will see that it's incomplete. There is one criterion — file not found. But how to determine that the document is "empty".
I think the problem is: there is no such concept as "empty" Excel spreadsheet. OP needs to define it, apply, or just ignore this criterion.

I actually put an answer...
—SA
ZurdoDev 14-Aug-13 11:59am    
No, the OP said, "I'm trying to find a way to prevent a user from canceling or leaving the excel filename blank."
Sergey Alexandrovich Kryukov 14-Aug-13 12:06pm    
Why "no"? I commented on your comment to my answer.
—SA

1 solution

So, why ignoring "empty" documents? Is it really important. I don't think people create empty documents and save as files intentionally. The probability of it is hardly much more than the probability of totally irrelevant or even random Excel documents.

So, please look at my comment to the question. The real problem is that Excel does not support the concept of "empty document" (unlike plain text files, for example). You need to think if this criterion makes sense. I doubt it.

[EDIT]

Probably I misunderstood you. Thanks to ryanb31 for pointing it out. Then the problem is much simpler, it is just reduced to UI. When you say "the program will ask the user", you should ask properly. The standard file open dialogs return the value which reflects the user's decision. When Cancel was pressed, you know that from return value. The dialog won't allow user to select no files without cancellation.

As simple as that.

—SA
 
Share this answer
 
v2
Comments
ZurdoDev 14-Aug-13 12:00pm    
The title suggests a blank file but if you read the notes the OP says, "I'm trying to find a way to prevent a user from canceling or leaving the excel filename blank."
Sergey Alexandrovich Kryukov 14-Aug-13 12:05pm    
Yes, yes, I know. I just don't think the API offers the method to identify such file without any analysis. And it makes messing with it not very sensible. Do you see the point?
—SA
ZurdoDev 14-Aug-13 12:10pm    
No. They are not asking how to know if the file is empty, but if the filename is not supplied by the user.
Sergey Alexandrovich Kryukov 14-Aug-13 12:14pm    
Or really? I missed it, thank you for the note. In this case, the answer would be simple...
—SA
Sergey Alexandrovich Kryukov 14-Aug-13 12:18pm    
So, I fixed the answer, after [EDIT].
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900