Reputation: 121
I can't figure out why i keep getting this warning message for the following variables: 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
There is nothing in the code that would push it out of the sub or skip the line where the first error is. Any assistance is appreciated.
Upvotes: 0
Views: 286
Reputation: 1151
To fix this, you should repeat your test on tempfilepath in the bypassthis block...
bypassthis:
If tempfilepath <> "" Then
'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
End If
As Alex B. mentioned in the comments, please be careful with GoTo - it is what caused your issue.
Upvotes: 1