User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 391,170 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,468 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Views: 145294 | Replies: 23 | Solved
Reply
Join Date: Aug 2004
Posts: 1
Reputation: dtbn is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dtbn dtbn is offline Offline
Newbie Poster

Open Excel file from Visual Basic

  #1  
Aug 13th, 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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: Adelaide, Australia
Posts: 191
Reputation: mnemtsas is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 1
mnemtsas's Avatar
mnemtsas mnemtsas is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

  #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:

  Dim xlTmp As Excel.Application
  
  Set xlTmp = New Excel.Application
  xlTmp.Workbooks.Open "MyXL.xls"

Cheers,
Reply With Quote  
Join Date: Mar 2006
Posts: 104
Reputation: manal is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 12
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

  #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  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Open Excel file from Visual Basic

  #4  
Mar 9th, 2006
varname=xlsheet.Cells(1, 1).value?
Reply With Quote  
Join Date: Mar 2006
Posts: 104
Reputation: manal is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 12
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

  #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  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: Open Excel file from Visual Basic

  #6  
Mar 9th, 2006
Is cell 1, 1 empty?
Reply With Quote  
Join Date: Mar 2006
Posts: 104
Reputation: manal is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 12
manal's Avatar
manal manal is offline Offline
Junior Poster

Re: Open Excel file from Visual Basic

  #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  
Join Date: Mar 2006
Posts: 5
Reputation: seagull is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
seagull seagull is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

  #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  
Join Date: Mar 2006
Posts: 6
Reputation: harry.net is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
harry.net harry.net is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

  #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.

Private Sub Form_Load()
Dim xlTmp As Excel.Application
Set xlTmp = New Excel.Application
xlTmp.Workbooks.Open "C:\Book1.xls"
Dim xlSht As Excel.Worksheet
Set xlSht = xlTmp.Sheets(1)
ReDim Preserve Ucode2(50, 5)
For i = 2 To xlSht.Cells.Rows
    Ucode2(i, 0) = xlSht.Cells(i, 1)
    Ucode2(i, 1) = xlSht.Cells(i, 2)
Next

xlTmp.Workbooks.Close
xlTmp.Quit

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

Function show1()

For i = 0 To UBound(Ucode2)
    textb0x3 = TextBox3 + Ucode2(i, 0)
    TextBox2 = TextBox2 + Ucode2(i, 1)
Next
End Function

and I called this function by this button click

Private Sub CommandButton1_Click()
Call show1
End Sub
Reply With Quote  
Join Date: Mar 2006
Posts: 5
Reputation: seagull is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 1
seagull seagull is offline Offline
Newbie Poster

Re: Open Excel file from Visual Basic

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 3 (0 members and 3 guests)

 

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 7:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC