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

Get specific CELL data from Ms Excel 2003 to display in TextBox.txt vb.net 2003.

Hi Experts,

I want to get specific CELL data from Ms Excel 2003 to display in TextBox.txt vb.net 2003.

I've Added Microsoft Excell 11.0 Object Library to References. and copied an example script from a book:
-----------
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet

sub ReadFile()
    Set xlApp = New Excel.Application
    xlApp.Visible = False

    For I = 0 To File1.ListCount - 1
      Set xlBook = xlApp.Workbooks.Open(File1.List(I))
      Set xlSheet = xlBook.Sheets("SheetA")
       :
      extract cells data here
       :
      xlApp.DisplayAlerts = False
      xlBook.Close
    Next

    xlApp.Quit
    Set xlApp = Nothing
    Set xlBook = Nothing
    Set xlSheet = Nothing

End Sub
-----------------------
A few error appear. Can anyone assist.
0
RobertoFreemano
Asked:
RobertoFreemano
  • 5
  • 3
  • 2
1 Solution
 
TerryInOhioCommented:
Here is sample code I have used to bring values for an excel workssheet into a VB6 program:

Dim oExcelApp
Dim oWorkbook
Dim irows
Dim icols
Dim xlwks
Dim xlrange
Set oExcelApp = CreateObject("Excel.application")
Set oWorkbook = oExcelApp.workbooks.open("C:\users.xls")
Set xlwks = oWorkbook.worksheets(1)
Set xlrange = xlwks.range("A1:C10")
For irows = 1 To 4
    For icols = 1 To 1
        MsgBox xlrange.cells(irows, icols)
    Next icols
Next irows
0
 
RobertoFreemanoAuthor Commented:
When I tried the above code... I get an 'Unhandled exception of type 'System.ArgumentException' error message & 'MsgBox xlrange.cells(irows, icols)' is highligthed.

Any ideas?
0
 
TerryInOhioCommented:
Did you make file users.xls and put data in?  Try commenting that line out with the msgbox, I don't use vbnet, maybe it would only be that statement it doesn't like.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RobertoFreemanoAuthor Commented:
Hi,

Yeah I tried that (forgot to Post that)... what happens is, it does start Excel.exe in the background as displayed on the Process list in Task Manager, but no application is visible to the eye. and no data is imported into a textbox.

I added a textbox  = TextBox1.Text = xlwks ("A1")... but I keep getting error messages.

:(
0
 
amiableansariCommented:
use this code to get the value of a particular cell

Dim excelCell As Excel.Range = CType(excelWorksheet.get_Range("A1", "A1"), Excel.Range)
Dim mValue As String = excelCell.Text

0
 
RobertoFreemanoAuthor Commented:
Hi amiableansari,

I've inserted your code but I get ')' expected message.

Could someone provide a full vb code for this request as I'm going round in circles.

Cheers.
0
 
amiableansariCommented:

try this one

        Dim oExcelApp
        Dim oWorkbook
        Dim xlwks As Excel.Worksheet
        Dim xlrange As Excel.Range
        oExcelApp = CreateObject("Excel.application")
        oWorkbook = oExcelApp.workbooks.open("d:\test.xls")
        xlwks = oWorkbook.worksheets(1)
        xlrange = xlwks.Range("B2:B2")

        MsgBox(xlrange.Text)

0
 
RobertoFreemanoAuthor Commented:
That's great amiableansari.

I will award you the points...

Is there a way of terminating Excel.exe after the cell data appears in a text file? as it create multiple processes in Task Manager evrytime I test it.

Dim oExcelApp
        Dim oWorkbook
        Dim xlwks As Excel.Worksheet
        Dim xlrange As Excel.Range
        oExcelApp = CreateObject("Excel.application")
        oWorkbook = oExcelApp.workbooks.open("c:\test.xls")
        xlwks = oWorkbook.worksheets(1)
        xlrange = xlwks.Range("B2:B2")
        TextBox1.Text = xlrange.Text()

or is vb running a direct feed from speadsheet?  Would it be possible to Copy to clipboard data from cell and Paste it into Textbox and close Excel Process to save CPU/Memory.

Cheers,
0
 
RobertoFreemanoAuthor Commented:
Hi,

I found a method to kill the process, but iy terminates my vb form  :(

Dim myprocess() As Process = Process.GetProcesses
Dim myProcess As Process
For Each myProcess In myProcesses
   If (myProcess.ProcessName.ToLower = "excel") Then
      myProcess.kill()
    End If
  Next
End Sub
0
 
amiableansariCommented:
Hi Robert

add this code when u finish working on excel. the excel process will automatically closed after application terminates.

Try
            oWorkbook.Close()
        Catch ex As Exception

            MsgBox(ex.ToString)
        End Try

        Try
            oExcelApp.Quit()
        Catch ex As Exception

            MsgBox(ex.ToString)
        End Try

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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