9 Replies - 4382 Views - Last Post: 24 September 2011 - 10:44 AM Rate Topic: -----

#1 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Object reference not set to an instance of an object.

Posted 23 September 2011 - 02:52 AM

Hi all i have a bug i need help with. i get the following error : Object reference not set to an instance of an object. i am get this error because of this line
Dim xlWorkBook As Excel.Workbook
i know it is because i have not wrote the line like so
Dim xlWorkBook As New Excel.Workbook
. the reason i don't just write like that is because if i do i get a com error which is: An error occurred creating the form. See Exception.InnerException for details. The error is: Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154. The program works ok i just would like to sort this bug out. any idea what could cause this com error. i am using excel ref 12.0

Is This A Good Question/Topic? 0
  • +

Replies To: Object reference not set to an instance of an object.

#2 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 03:44 AM

Perhaps this will work on your end:

 Dim xlApp As New Excel.Application
        Dim xlWorkbook As Excel.Workbook = Nothing
       

        Try
            xlApp.Visible = True
            xlWorkbook = xlApp.Workbooks.Open("C:\blah.xls")
       Catch......

Was This Post Helpful? 0
  • +
  • -

#3 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 06:31 AM

Dim WkBk As Excel.Workbook = excelApp.Workbooks.Open("C:\yourfile.xls", 0, False, 5, "", "", False, Excel.XlPlatform.xlWindows, "", True, False, 0, True, False, False)



This is the code to use if all you want is to open an existing workbook. Check out this blog detailing my learning to deal with Excel in VB.NET.
Was This Post Helpful? 1
  • +
  • -

#4 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 07:47 AM

Hi thanks for the reply i trid setting it to nothing that didn't work still get Object reference not set to an instance of an object error. I then started to look at your idea psyguy but i would like to set the file name to openfileDialog file name for example :

'Dim xlWorkBook As Excel.Workbook = xlApp.Workbooks.Open(openFileDialog1.FileName, 0, False, 5, "", "", False, Excel.XlPlatform.xlWindows, "", True, False, 0, True, False, False)


but i get this error:

An error occurred creating the form. See Exception.InnerException for details.  The error is: '' could not be found. Check the spelling of the file name, and verify that the file location is correct.

If you are trying to open the file from your list of most recently used files, make sure that the file has not been renamed, moved, or deleted.


I think this is because it is looking for the file name on start up any work around for this
Was This Post Helpful? 0
  • +
  • -

#5 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 08:09 AM

I cringed when I saw the title of this thread...a Null Reference error is sooo simple to fix.
But you already know what to do to fix the Null Reference error: use new to instantiate a new instance of the object before you attempt to use it.

You should have posted the real problem as your title!

Anyways a 80040154 error either means that you haven't installed the the Excel dll properly on your computer or that the user running the application does not have the appropriate permissions to access that dll.

Make sure that you have Excel installed on the computer running your application!

-Frinny

This post has been edited by Frinavale: 23 September 2011 - 08:12 AM

Was This Post Helpful? 0
  • +
  • -

#6 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 08:22 AM

please explaine this: regsvr32 tool to install COM objects. have never heard of that.
Was This Post Helpful? 0
  • +
  • -

#7 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Object reference not set to an instance of an object.

Posted 23 September 2011 - 08:50 AM

Well if I were you I'd first try just installing Excel on the computer.
That should fix the problem.

If not Regsvr32 tool is a command line tool that comes with the Windows Operating System that will register the DLL with the operating system (making a registry entry etc) so that your software can locate the resource on the computer.

So, first try installing Excel....because it will probably do a better job of this than the regsvr32 tool.

Just thought I should also post a link to the Regasm tool also.

The Regasm tool registers .NET assemblies...whereas the Regsvr32 tool is used for COM/ActiveX stuff.

This post has been edited by Frinavale: 23 September 2011 - 08:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 m_wylie85  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 96
  • View blog
  • Posts: 899
  • Joined: 15-October 10

Re: Object reference not set to an instance of an object.

Posted 24 September 2011 - 05:12 AM

I don't mean to be cheeky but do you really think i would try to output to excel without having it installed
Was This Post Helpful? 0
  • +
  • -

#9 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Object reference not set to an instance of an object.

Posted 24 September 2011 - 09:08 AM

I'm sorry, as stupid as it sounds, some people actually do try this kind of thing!

If Excel is installed then you need to check permissions on the DLL that you're using to make sure that the user running the application has access to it.

I see this error all the time...one of my web applications that uses a VB6 DLL... I see this error when I don't register the DLL (using the regsvr32 tool) or I forget to give the ASPNet user account permissions to this DLL.

The worst part is when I see this error after I register the DLL and give the appropriate permissions because then I have to un-register it, go through the registry and remove it from there (in multiple places), and then re-register it and give the appropriate permissions. This process takes a lot more time.

-Frinny

This post has been edited by Frinavale: 24 September 2011 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#10 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Object reference not set to an instance of an object.

Posted 24 September 2011 - 10:44 AM

Things to think about; does the object library match the Excel version, if Frinavale works with these types and is experienced in this error wouldn't his suggestion be worth trying? I can say I have not had this problem when working with Excel objects.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1