为什么我会收到错误BC42104变量在分配之前使用?

时间:2016-03-25 19:21:26

标签: vb.net visual-studio-2015

我无法弄清楚为什么我不断收到以下变量的警告消息:xlApp,xlWorkBooks,xlWorkSheet,xlWorkSheets,xlWorkBook。

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    'PRE-PROCESSING VALIDATIONS
    Button2.Visible = False
    Button6.Visible = False

    If perennialcounter + annualcounter >= 6 Then
        MsgBox("No more files can be loaded")
        GoTo BypassThis
    End If

    'select and open file

    'file selection box and error handling for no file selected/cancel button
    TempOpenFileDialog.Title = "Select an Input File"
    If Testing = True Then
        TempOpenFileDialog.InitialDirectory = TestingFileDefaultDirectory
    Else
        TempOpenFileDialog.InitialDirectory = TallySheetOrderFileDefaultDirectory
    End If
    TempOpenFileDialog.Multiselect = False
    TempOpenFileDialog.ShowDialog()
    tempfilepath = TempOpenFileDialog.FileName()
    tempfile = Path.GetFileName(tempfilepath)
    If tempfilepath = "" Then
        GoTo BypassThis
    End If

    'open file
    Dim xlApp As Excel.Application = Nothing
    Dim xlWorkBooks As Excel.Workbooks = Nothing
    Dim xlWorkBook As Excel.Workbook = Nothing
    Dim xlWorkSheets As Excel.Worksheets = Nothing
    Dim xlWorkSheet As Excel.Worksheet = Nothing
    xlApp = New Excel.Application
    xlApp.DisplayAlerts = False
    xlWorkBooks = xlApp.Workbooks
    xlWorkBooks.Open(tempfilepath)
    If Testing = True Then
        xlApp.Visible = True
    Else
        xlApp.Visible = False
    End If
    xlWorkBooks(tempfile).Activate()
    xlWorkSheet = xlApp.ActiveWorkbook.ActiveSheet

    'lots and lots of code...
bypassthis:
    'close excel (save) and release objects
    xlhwnd = xlApp.Hwnd    '<------ warning here
    ProcIDxl = 0
    xproc = Process.GetProcessById(ProcIDxl)
    GetWindowThreadProcessId(xlhwnd, ProcIDxl)

    xlWorkBooks(tempfile).Close(True)    '<------ warning here
    xlApp.Application.Quit()

    GC.Collect()
    GC.WaitForPendingFinalizers()

    releaseObject(xlWorkSheet)    '<------ warning here
    releaseObject(xlWorkSheets)    '<------ warning here
    releaseObject(xlWorkBook)    '<------ warning here
    releaseObject(xlWorkBooks)
    releaseObject(xlApp)

    xlWorkSheet = Nothing
    xlWorkSheets = Nothing
    xlWorkBook = Nothing
    xlWorkBooks = Nothing
    xlApp = Nothing


    'show/reshow buttons
    If perennialcounter + annualcounter > 0 Then
        Button6.Visible = True
        Button6.Text = "Process"
    End If
    Button2.Visible = True
    ProgressBar7.Visible = False
    ProgressBar8.Visible = False
End Sub

代码中没有任何内容可以将其推出sub或跳过第一个错误所在的行。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

要解决此问题,您应该在旁路阻止的块中重复测试tempfile路径...

ActiveCell.FormulaR1C1 = "=VLOOKUP(""Sugar (g)"", " &  _
                      wb.Worksheets(1).Range("A18:F28").address(External:=true, ReferenceStyle:=xlR1C1) & _
                      ", 2, FALSE)"
'or,
ActiveCell.Formula = "=VLOOKUP(""Sugar (g)"", " &  _
                      wb.Worksheets(1).Range("A18:F28").address(External:=true) & _
                      ", 2, FALSE)"

正如Alex B.在评论中提到的那样,请小心使用GoTo - 这是导致您出现问题的原因。