codedemon
New member
- Joined
- Oct 2, 2012
- Messages
- 1
- Programming Experience
- 1-3
I am working on a program that generates serial#s for our machines, it will read and write data to an excel spreadsheet.
When the form loads:
I notice that the Excel sheet doesnt quit until I close the application. If I want to use the same Excel sheet in another Sub, it opens another instance of Excel.
Example: (I inserted a Msgbox just for testing)
How can I quit the instance of the Excel sheet and reopen it later when I need to write to it? Should I leave it open until the application is closed? If so, how do I prevent multiple instances from opening?
When the form loads:
VB.NET:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
GetSheetNames()
End Sub
VB.NET:
Public Sub GetSheetNames()
Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim totalWorkSheets As Excel.Worksheet
Dim objWorkSheets As Excel.Worksheet
Dim ExcelSheetName As String = ""
objExcel = CreateObject("Excel.Application")
objWorkBook = objExcel.Workbooks.Open("C:\VBNET\master-serial-list.xlsx")
ComboBox1.Items.Clear()
' this code gets the names off all the worksheets
For Each totalWorkSheets In objWorkBook.Worksheets
'ExcelSheetName += totalWorkSheets.Name
ComboBox1.Items.Add(totalWorkSheets.Name)
Next totalWorkSheets
objExcel.Quit()
End Sub
I notice that the Excel sheet doesnt quit until I close the application. If I want to use the same Excel sheet in another Sub, it opens another instance of Excel.
Example: (I inserted a Msgbox just for testing)
VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
GetHistory()
End Sub
Public Sub GetHistory() Dim objExcel As Excel.Application
Dim objWorkBook As Excel.Workbook
Dim totalWorkSheets As Excel.Worksheet
Dim objWorkSheets As Excel.Worksheet
Dim ExcelSheetName As String = ""
objExcel = CreateObject("Excel.Application")
objWorkBook = objExcel.Workbooks.Open("C:\VBNET\master-serial-list.xlsx")
MsgBox(ComboBox1.ValueMember)
objExcel.Quit()
End Sub
How can I quit the instance of the Excel sheet and reopen it later when I need to write to it? Should I leave it open until the application is closed? If so, how do I prevent multiple instances from opening?