| | |
Open Excel file from Visual Basic
Thread Solved
![]() |
•
•
•
•
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)
Dim xlTmp As Excel.Application Set xlTmp = New Excel.Application xlTmp.Workbooks.Open "MyXL.xls"
Cheers,
Mark Nemtsas
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
Time and Billing Software - Time Tracking Software - Roller Shutters - Roller Blinds -
Baby Books
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 1
•
•
•
•
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
•
•
Join Date: Mar 2006
Posts: 6
Reputation:
Solved Threads: 1
•
•
•
•
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)
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 :
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
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
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
Private Sub CommandButton1_Click() Call show1 End Sub
•
•
Join Date: Mar 2006
Posts: 5
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- opening excel file in visual basic 6 (Visual Basic 4 / 5 / 6)
- getting data from a text file and putting it in an excel file using visual basic 6.0 (Visual Basic 4 / 5 / 6)
- open a PDF file in visual basic (Visual Basic 4 / 5 / 6)
Other Threads in the Visual Basic 4 / 5 / 6 Forum
- Previous Thread: SPI data transfer to Atmega8 by VB6 through parallel port
- Next Thread: winsock question
| Thread Tools | Search this Thread |
* 6 2007 access activex add age basic beginner birth bmp calculator cd cells.find click client code college connection connectionproblemusingvb6usingoledb creat ctrl+f data database datareport date delete dissertations dissertationthesis dissertationtopic edit excel excelmacro file filename form header iamthwee image inboxinvb internetfiledownload listbox listview liveperson login looping microsoft movingranges number objectinsert open oracle password prime program prompt range-objects readfile reading remotesqlserverdatabase report save search sendbyte sites sql sql2008 sqlserver subroutine tags time urldownloadtofile vb vb6 vb6.0 vba visual visualbasic visualbasic6 web windows






