尝试声明没有捕获异常vb.net

时间:2015-09-30 02:40:54

标签: vb.net

我的例外情况不适用于搜索并返回信息的excel文件。下面您将看到它循环遍历第一列并根据全局设置的变量返回信息。我提出了一个例外,如果尝试失败,将弹出一个消息框并告诉他们找不到它们,但它没有抓住它。相反,如果用户未在数据库中列出,则冻结程序。

    Private Sub ExceptionToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExceptionToolStripMenuItem.Click

    Dim objexcel As New Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet
    objexcel.DisplayAlerts = False

    If Global_Variables.SavedResults.UserM = Nothing And Global_Variables.SavedResults.UserL = Nothing Then
        objWorkbook = objexcel.Workbooks.Open("filepath", [ReadOnly]:=True)

        objWorksheet = CType(objWorkbook.Worksheets.Item("Sheet1"), Excel.Worksheet)
        Try
            For x As Integer = 1 To objWorksheet.Rows.Count Step 1


                If objWorksheet.Cells(x, 1).value = Global_Variables.SavedResults.UserD Then

                    Global_Variables.SavedResults.UserL = objWorksheet.Cells(x, 3).value
                    Global_Variables.SavedResults.UserM = objWorksheet.Cells(x, 2).value

                    Exit For
                End If
            Next
            Exit Try


        Catch ex As Exception
            MsgBox("You are not in the Database.")
        End Try

        objWorkbook.Close()
        objexcel.Quit()

        ReleaseObject(objWorksheet)
        ReleaseObject(objWorkbook)
        ReleaseObject(objexcel)
        objWorkbook = Nothing
        objWorksheet = Nothing
        objexcel = Nothing
    End If

    Dim Email As New Email_Templates
    Email.Exception()
End Sub

0 个答案:

没有答案