Open Excel file from Visual Basic

Thread Solved

Join Date: Aug 2004
Posts: 1
Reputation: dtbn is an unknown quantity at this point 
Solved Threads: 0
dtbn dtbn is offline Offline
Newbie Poster

Open Excel file from Visual Basic

 
-1
  #1
Aug 14th, 2004
Hi,

I already have an excel file (MyXL.xls) and a Visual Basic (Form1.frm) with a command button. Now, I want to write a code for the Visual Basic so that when I click on the command button, the excel file will be opened.

Any help is significant to me! Thank you in advance.

DTBN
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 200
Reputation: mnemtsas is an unknown quantity at this point 
Solved Threads: 1
mnemtsas's Avatar
mnemtsas mnemtsas is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

 
0
  #2
Aug 15th, 2004
Originally Posted by dtbn
Hi,

I already have an excel file (MyXL.xls) and a Visual Basic (Form1.frm) with a command button. Now, I want to write a code for the Visual Basic so that when I click on the command button, the excel file will be opened.

Any help is significant to me! Thank you in advance.

DTBN

Include excel type library in the project references and then use the following:

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim xlTmp As Excel.Application
  2.  
  3. Set xlTmp = New Excel.Application
  4. xlTmp.Workbooks.Open "MyXL.xls"

Cheers,
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

 
0
  #3
Mar 9th, 2006
hi
I want to know how to read from excel file
i know how to open it but how to store cell's value from excel to the variable
i wrote
varname=xlsheet.Cells(1, 1)

and i got run timeerror
"cast from range to "string not supported
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Open Excel file from Visual Basic

 
0
  #4
Mar 9th, 2006
varname=xlsheet.Cells(1, 1).value?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

 
0
  #5
Mar 9th, 2006
Originally Posted by Comatose
varname=xlsheet.Cells(1, 1).value?
thanks....
i don't have now run time error
but i tried to display result on textbox i always have empty textbox???
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Open Excel file from Visual Basic

 
0
  #6
Mar 9th, 2006
Is cell 1, 1 empty?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 121
Reputation: manal is an unknown quantity at this point 
Solved Threads: 17
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

 
0
  #7
Mar 9th, 2006
No......it dose have value
i want to write program that read the names of student of specific section from excel file and display it in textbox

actually each cell has name of student
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: seagull is an unknown quantity at this point 
Solved Threads: 1
seagull seagull is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

 
0
  #8
Mar 22nd, 2006
Originally Posted by manal
No......it dose have value
i want to write program that read the names of student of specific section from excel file and display it in textbox

actually each cell has name of student

Not sure if you've solved this yet. I had exactly the same problem. Got this to work eventually. I needed to explicitly declare a1 as a range

Dim xlApp As Excel.Application = New Excel.Application
xlApp.DisplayAlerts = False
xlApp.Workbooks.Open("c:\test.xls")
Dim xlSht As Excel.Worksheet = xlApp.Sheets(1)
Dim xlRng As Excel.Range = xlSht.Cells(1, 1)
Textbox1.Text = xlRng.Value

Dan
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 6
Reputation: harry.net is an unknown quantity at this point 
Solved Threads: 1
harry.net harry.net is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

 
0
  #9
Mar 29th, 2006
Originally Posted by seagull
Not sure if you've solved this yet. I had exactly the same problem. Got this to work eventually. I needed to explicitly declare a1 as a range

Dim xlApp As Excel.Application = New Excel.Application
xlApp.DisplayAlerts = False
xlApp.Workbooks.Open("c:\test.xls")
Dim xlSht As Excel.Worksheet = xlApp.Sheets(1)
Dim xlRng As Excel.Range = xlSht.Cells(1, 1)
Textbox1.Text = xlRng.Value

Dan

I used your code to open an Excel file and store its cells in an array
but it gives me run_time error7 : out of memory

and when I check the task manager Excel.EXE is still running and the computer become slow.

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub Form_Load()
  2. Dim xlTmp As Excel.Application
  3. Set xlTmp = New Excel.Application
  4. xlTmp.Workbooks.Open "C:\Book1.xls"
  5. Dim xlSht As Excel.Worksheet
  6. Set xlSht = xlTmp.Sheets(1)
  7. ReDim Preserve Ucode2(50, 5)
  8. For i = 2 To xlSht.Cells.Rows
  9. Ucode2(i, 0) = xlSht.Cells(i, 1)
  10. Ucode2(i, 1) = xlSht.Cells(i, 2)
  11. Next
  12.  
  13. xlTmp.Workbooks.Close
  14. xlTmp.Quit

and I wanted to display this array in two different boxes :

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Function show1()
  2.  
  3. For i = 0 To UBound(Ucode2)
  4. textb0x3 = TextBox3 + Ucode2(i, 0)
  5. TextBox2 = TextBox2 + Ucode2(i, 1)
  6. Next
  7. End Function

and I called this function by this button click

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Private Sub CommandButton1_Click()
  2. Call show1
  3. End Sub
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 5
Reputation: seagull is an unknown quantity at this point 
Solved Threads: 1
seagull seagull is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

 
0
  #10
Mar 29th, 2006
Sorry,

I did a bad thing. I just put part of the code. You should release all the COM objects when done with them in reverse order. That's every workbook, sheet, range etc. something like:

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Dim xlApp As Excel.Application
Dim xlSht As Excel.Worksheet
Dim xlRng As Excel.Range

Try
xlApp = New Excel.Application
xlApp.DisplayAlerts = False
xlApp.Workbooks.Open("c:\test.xls")
xlSht = xlApp.Sheets(1)
xlRng = xlSht.Cells(1, 1)
Textbox1.Text = xlRng.Value

Catch ex As Exception
Textbox1.Text &= ex.ToString
Finally
xlApp.Workbooks.Close()
ReleaseComObject(xlRng)
ReleaseComObject(xlSht)
ReleaseComObject(xlApp)
xlSht = Nothing
xlApp = Nothing
GC.Collect()
End Try
End Sub


Dan
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC