Opening Excel Worksheet - "Object reference not set to an instance of an object."

X

xhenxhe

I don't some Excel automation. I've created a program that opens and Excel
template and inputs information to it. It runs great on my machine. When I
build and deploy I have a user that keep getting the error message: "Object
reference not set to an instance of an object."

Here is the code that generates the message:
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

Try
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
Catch ex As Exception
MsgBox("Could not create Excel Application Object: " & ex.Message,
MsgBoxStyle.Critical, Title)
Exit Sub
End Try

Try
xlBook = CType(xlApp.Workbooks.Open(txtCommonTemplate.Text),
Excel.Workbook)
Catch ex As Exception
MsgBox("Could not create instance of Excel Workbook: " & ex.Message,
MsgBoxStyle.Critical, Title)
Exit Sub
End Try

Try
xlSheet = CType(xlBook.Worksheets(1), Excel.Worksheet)
Catch ex As Exception
MsgBox("Could not create instance of an Excel Worksheet: " & ex.Message,
MsgBoxStyle.Critical, Title)
Exit Sub
End Try

====

It is the second Try/Catch that displays the message - trying to open the
worksheet.

I've tried different variants of this code (new instead of ctype) which all
product the same results.

The user having problems is running office 2000. I'm using Office 2002. I'm
assuming this is the problem. Does anyone have a solution (other than me
downgrading or the user upgrading)?

Thanks,
Dustin
 
G

Guest

For some statements to work with Excel, you have to make the worksheet active;

xlsheet.Activate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top