943,614 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Aug 14th, 2004
0

Open Excel file from Visual Basic

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dtbn is offline Offline
1 posts
since Aug 2004
Aug 15th, 2004
0

Re: Open Excel file from Visual Basic

Quote 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,
Reputation Points: 16
Solved Threads: 1
Posting Whiz in Training
mnemtsas is offline Offline
200 posts
since Jul 2004
Mar 9th, 2006
0

Re: Open Excel file from Visual Basic

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
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Mar 9th, 2006
0

Re: Open Excel file from Visual Basic

varname=xlsheet.Cells(1, 1).value?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 9th, 2006
0

Re: Open Excel file from Visual Basic

Quote 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???
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Mar 9th, 2006
0

Re: Open Excel file from Visual Basic

Is cell 1, 1 empty?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 9th, 2006
0

Re: Open Excel file from Visual Basic

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
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Mar 22nd, 2006
0

Re: Open Excel file from Visual Basic

Quote 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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
seagull is offline Offline
5 posts
since Mar 2006
Mar 29th, 2006
0

Re: Open Excel file from Visual Basic

Quote 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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
harry.net is offline Offline
6 posts
since Mar 2006
Mar 29th, 2006
0

Re: Open Excel file from Visual Basic

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
seagull is offline Offline
5 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: VBA Validation Module
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: VB procedure for GCD





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC