Link to home
Start Free TrialLog in
Avatar of chwong67
chwong67Flag for Malaysia

asked on

System.NullReferenceException for Excel.Workbooks.Open (visual Studio

Visual Studio 2003 error message.
Error message prompted "System.NullReferenceException: Object reference not set to an instance of an object.  at Microsoft.Office.Interop.Excel.Workbooks.Open" while open excel at user's computer (MS XP Service Pack 3, Office 2000).

It work fine at my development computer (NS XP Service Pack 2, Office 2007).

Part of coding:

Imports Microsoft.Office.Interop

Dim App As New Excel.Application
App.Visible = False
App.Application.SheetsInNewWorkbook = 1
Dim Destination As Excel.Workbook = App.Workbooks.Open("C:\Test.xls")

Any help is very much appreciated...
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Try this:

Dim Destination As NEW Excel.Workbook = App.Workbooks.Open("C:\Test.xls")

Example below with error-handling
Private Sub OpenExcelFileForEditing(ByVal strFilePath As String)
        Dim App As New Excel.Application
        Dim Destination As New Excel.Workbook 
        App.Visible = False
        App.Application.SheetsInNewWorkbook = 1

        'Open the workbook
        Try
            Destination = App.Workbooks.Open("C:\Test.xls")
        Catch ex As Exception
            MsgBox("Exception:  Model import, could not open model Design Table file: C:\Test.xls")
            Return
        End Try
    End Sub

Open in new window

Please check that your client's Office has all the service packs.

By the way if your code is compiled for Office 2007 (version 12.0) then it will not work for Office 2000. The PIA (or com interfaces are different and not compatible)

If you need to support Office 2000, you need to compile your program with the office 2000 object library (version 9.0) meaning changing the excel references to point to the excel 2000 com components.


Avatar of chwong67

ASKER

Thanks for your prompt respond.
I re-install Miscoroft Office 2000 for Development computer and also intalled Office XP PIAs.
I faced the error message "QueryInterface for interface Excel._Application failed" for 'App.Visible = False' line.






Office 2000 (v9) do not use Office xp pia (v10).
Please point to the right version of com references.

Note:
By the way it is not a good idea to install /reinstall Office versions without clearing the registry from the previous Office entries as it may meess up your system. You are better off using a virtual machine (VMware) to do this stuff.
Kindly advice on the right version of PIA that I can select for Excel.application. Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your time. It's working now.