Solved

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

Posted on 2006-10-27
10
269 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:RobertoFreemano
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:RobertoFreemano
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
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.
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now