?
Solved

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

Posted on 2006-10-27
10
Medium Priority
?
276 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 

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 180 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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 …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

762 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