excel - Null reference exception when trying to open a workbook, vb.net -


i've got openfiledialog reading spreadsheet file name textbox, performing formatting , spitting out text file. code works fine 1 time through; next task can open successive spreadsheets (one @ time) without closing program.

when try open second excel file, null reference exception (object ref not set instance of object) on line i'm opening workbook.

public class form1

dim xlapp new microsoft.office.interop.excel.application dim xlworkbook, xlworkbook2 microsoft.office.interop.excel.workbook dim xlwsheet, xlwsheet2 microsoft.office.interop.excel.worksheet dim strm system.io.stream dim formfile string = "c:\nitemp.tmp\quantdata.xls"  private sub open_click(sender object, e eventargs) handles open.click     'open button code'     openfiledialog1.title = "select file"     openfiledialog1.initialdirectory = directory.text 'uppermost text box, change open different default directory open button'     openfiledialog1.restoredirectory = true     openfiledialog1.showdialog() end sub   private sub openfiledialog1_fileok(sender object, e system.componentmodel.canceleventargs) handles openfiledialog1.fileok     dim lrow integer = 0      try         strm = openfiledialog1.openfile()         textbox1.text = openfiledialog1.filename.tostring()         xlworkbook = xlapp.workbooks.open(textbox1.text) 'opens excel file'         xlapp.visible = false         strm.close() 

i see 2 possible null references here: 1) .tostring on previous line may empty. wi run code, textbox isn't being populated correct file path. after error box pops , hit 'continue' textbox show correct path. 2) issue platform, 64x vs 32x? came in search, tried "new excel.app" , "new excel.workbook", changed x86 platform, yet got me infamous com exception 80040154, makes me think not issue, i'm still pretty new coding..

can find null?

first things first, don't need call openfiledialog1.openfile(). in fact don't need filestream @ aren't manipulating file directly (only excel is).

secondly, need retrieve , dispose of xlapp.workbooks collection independently, otherwise going leak com wrappers. null reference exception either workbooks collection being null, or open filename being null. error handling solve problem.

... dim xlworkbooks excel.workbooks dim xlworkbookopened excel.workbook  try     textbox1.text = openfiledialog1.filename.tostring()     if (textbox1.text isnot nothing)         xlworkbooks = xlapp.workbooks         if (xlworkbooks isnot nothing)             xlworkbookopened = xlworkbooks.open(textbox1.text) 'opens excel file'             if (xlworkbookopened isnot nothing)                 ' whatever need to...                         marshal.releasecomobject(xlworkbookopened)                 xlworkbookopened = nothing             end if             marshal.releasecomobject(xlworkbooks)             xlworkbooks = nothing         end if     end if  catch ex exception     ' log error start with...     trace.writeline(ex.message) end try 

note i've explicitly released every com object after use , set value nothing. necessary ensure proper cleanup.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -