当用户不选择任何file按cancel后会出现(Sorry, we couldn't find . Is it possible it was moved, renamed or deleted?)
path = OpenFile()
If path = "" Then
Me.restart()
End If
path = OpenFile()
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()
Dim excelBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open(path)
Dim excelSheets As String() = New String(excelBook.Worksheets.Count - 1) {}
Dim i As Integer = 0
For Each wSheet As Microsoft.Office.Interop.Excel.Worksheet In excelBook.Worksheets
excelSheets(i) = wSheet.Name
i += 1
Next
ComboBox1.Items.Clear()
ComboBox1.Items.AddRange(excelSheets)
End Sub
Private Function OpenFile() As String
Dim OFD As New OpenFileDialog
With OFD
.AddExtension = True
.CheckFileExists = True
.Filter = "Excel files Excel files (*.xls)|*.xls"
.Multiselect = False
.Title = "Select an Excel to open"
End With
'show the ofd and if a file was selected return it, otherwise return nothing
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Return OFD.FileName
Else
Return Nothing
End If
End Function
想请教如何让用户在不选择任何文件的情况下,不出现这个error