Solved

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

Posted on 2006-10-27
10
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes

726 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