Solved

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

Posted on 2006-10-27
10
270 Views
Last Modified: 2010-04-16
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
Comment
Question by:RobertoFreemano
  • 5
  • 3
  • 2
10 Comments
 
LVL 1

Expert Comment

by:TerryInOhio
ID: 17822006
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
 

Author Comment

by:RobertoFreemano
ID: 17822219
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
 
LVL 1

Expert Comment

by:TerryInOhio
ID: 17822341
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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:RobertoFreemano
ID: 17822483
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
 
LVL 2

Expert Comment

by:amiableansari
ID: 17825027
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
 

Author Comment

by:RobertoFreemano
ID: 17829487
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
 
LVL 2

Accepted Solution

by:
amiableansari earned 60 total points
ID: 17832481

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
 

Author Comment

by:RobertoFreemano
ID: 17843792
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
 

Author Comment

by:RobertoFreemano
ID: 17844538
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
 
LVL 2

Expert Comment

by:amiableansari
ID: 17847991
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why my code login on a web site with access vba don't work on microsoft access 2016 26 65
Help Required 3 91
Query Syntax 17 32
Query group by data in SQL Server - cursor? 3 29
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question