• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Using Excel 2003 in VB.net, having trouble importing data

Greetings,

I'm having a problem with VB.net in programming to import data either from an array or using QueryTables to import a .csv file.  To preface this, I haven't had much experience with Excel in programming.  

When I go to importing the data (currently trying the easier of the two methods (array)) yields me a "object reference not set to an instance of an object" error.

Here is the code that matters:

...
        Dim excelApp As New Excel.Application
        Dim excelBooks As Excel.Workbooks = excelApp.Workbooks
        Dim excelBook As Excel.Workbook = excelBooks.Add
        Dim excelWorksheet As Excel.Worksheet
        excelApp.Visible = False
        Dim excelRange As Excel.Range
        excelWorksheet = excelApp.ActiveSheet()

        Dim ctr As Long = 0
        Dim args As Object

        For ctr = 0 To ArrayCtr
            args = HoldingArray(ctr)
            excelRange.Cells((ctr + 4), 2) = args  'This is where the program throws the error
        Next
...

To clarify: HoldingArray is the global array that holds the data, and ArrayCtr is the static counter that holds the toprange of the array.

Help!
0
icywindow
Asked:
icywindow
  • 3
  • 2
1 Solution
 
rafranciscoCommented:
This is how I did it when working with Excel.  First you have to get the cell using the get_Range method of the worksheet:

excelRange = excelWorksheet.get_Range("A2", "A2")

Then to set the value, use the set_Value method of the excelRange:

excelRange.set_Value(null, args)
0
 
icywindowAuthor Commented:
That doesn't seem to work when working with vb.net 2003 and the excel 11 com library (Excel 2003), for it seems that excel.worksheet.get_Range() doesn't exist.  Perhaps it's a library corruption all along?

Thank you.
0
 
icywindowAuthor Commented:
Well, the libraries seem to be fine.  Should I go back to VB6 to make it work more easily, or should I use an earlier version of office?
Neither get_range or set_value exist, but the range and value properties exist.  Should I use those?

Thanks for your quick response
~icywindow
0
 
rafranciscoCommented:
I just checked my code and found that instead of set_value, I used Value2.

As for get_range, it do exist in the worksheet.  Does your code look like this?

Dim excelWorksheet As Excel.Worksheet
excelRange = excelWorksheet.get_Range("A2", "A2")

I am using Microsoft Office 11.0 Object Library.
0
 
icywindowAuthor Commented:
Well, it seems I found an easier way to do the same thing; that is using the worksheet.cells property, contextually

Excel.Worksheet.Cells(RowIndex As Object, ColumnIndex As Object) = args as (Any Data Type)

Either way, rafrancisco, I thank you for your response in a punctual fashion, you get the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now