How can i close multiple instances of excel using visual basic.
I am opening worksheets of a same excel workbook through vb and when i try to close them it closes only the last instances.
here's my code:
Dim xlCurrent As excel.Application
Dim wbCurrent As Workbook
Private Function openExcel()
Set xlCurrent = CreateObject("Excel.Application"
' Do not display Excel warning messages.
xlCurrent.DisplayAlerts = False
xlCurrent.DisplayFullScreen = True
xlCurrent.Visible = False
xlCurrent.CommandBars.ActiveMenuBar.Enabled = False
Set wbCurrent = xlCurrent.Workbooks.Open("C:\SoftvsROProgramCDNv1.02_Unlocked.xls"
End Function
Private Sub mnuASMEGuideLineTable_Click()
On Error Resume Next
'Set xlCurrent = GetObject(, "Excel.Application"
If xlCurrent Is Nothing Then
MsgBox "Excel Instance not found"
Exit Sub
Else
Call openExcel
End If
wbCurrent.Worksheets(12).Visible = True
xlCurrent.Visible = True
'Application.ScreenUpdating = False
wbCurrent.Worksheets(1).Visible = False
wbCurrent.Worksheets(2).Visible = False
wbCurrent.Worksheets(3).Visible = False
wbCurrent.Worksheets(4).Visible = False
wbCurrent.Worksheets(5).Visible = False
wbCurrent.Worksheets(6).Visible = False
wbCurrent.Worksheets(7).Visible = False
wbCurrent.Worksheets(8).Visible = False
wbCurrent.Worksheets(9).Visible = False
wbCurrent.Worksheets(10).Visible = False
wbCurrent.Worksheets(11).Visible = False
wbCurrent.Worksheets(13).Visible = False
wbCurrent.Worksheets(14).Visible = False
End Sub
Private Sub mnuCarbonTankTable_Click()
On Error Resume Next
'Set xlCurrent = GetObject(, "Excel.Application"
If xlCurrent Is Nothing Then
MsgBox "Excel Instance not found"
Exit Sub
Else
Call openExcel
End If
wbCurrent.Worksheets(9).Visible = True
xlCurrent.Visible = True
'Application.ScreenUpdating = False
wbCurrent.Worksheets(1).Visible = False
wbCurrent.Worksheets(2).Visible = False
wbCurrent.Worksheets(3).Visible = False
wbCurrent.Worksheets(4).Visible = False
wbCurrent.Worksheets(6).Visible = False
wbCurrent.Worksheets(7).Visible = False
wbCurrent.Worksheets(8).Visible = False
wbCurrent.Worksheets(5).Visible = False
wbCurrent.Worksheets(10).Visible = False
wbCurrent.Worksheets(11).Visible = False
wbCurrent.Worksheets(12).Visible = False
wbCurrent.Worksheets(13).Visible = False
wbCurrent.Worksheets(14).Visible = False
End Sub
Private Sub mnuCloseExcel_Click()
If Not xlCurrent Is Nothing Then
xlCurrent.DisplayAlerts = False
xlCurrent.CommandBars.ActiveMenuBar.Enabled = True
xlCurrent.DisplayFullScreen = True
xlCurrent.Quit
DoEvents
Set xlCurrent = Nothing
Set wbCurrent = Nothing
End Sub
please go through my code and check where i am wrong.
Thanks
I am opening worksheets of a same excel workbook through vb and when i try to close them it closes only the last instances.
here's my code:
Dim xlCurrent As excel.Application
Dim wbCurrent As Workbook
Private Function openExcel()
Set xlCurrent = CreateObject("Excel.Application"
' Do not display Excel warning messages.
xlCurrent.DisplayAlerts = False
xlCurrent.DisplayFullScreen = True
xlCurrent.Visible = False
xlCurrent.CommandBars.ActiveMenuBar.Enabled = False
Set wbCurrent = xlCurrent.Workbooks.Open("C:\SoftvsROProgramCDNv1.02_Unlocked.xls"
End Function
Private Sub mnuASMEGuideLineTable_Click()
On Error Resume Next
'Set xlCurrent = GetObject(, "Excel.Application"
If xlCurrent Is Nothing Then
MsgBox "Excel Instance not found"
Exit Sub
Else
Call openExcel
End If
wbCurrent.Worksheets(12).Visible = True
xlCurrent.Visible = True
'Application.ScreenUpdating = False
wbCurrent.Worksheets(1).Visible = False
wbCurrent.Worksheets(2).Visible = False
wbCurrent.Worksheets(3).Visible = False
wbCurrent.Worksheets(4).Visible = False
wbCurrent.Worksheets(5).Visible = False
wbCurrent.Worksheets(6).Visible = False
wbCurrent.Worksheets(7).Visible = False
wbCurrent.Worksheets(8).Visible = False
wbCurrent.Worksheets(9).Visible = False
wbCurrent.Worksheets(10).Visible = False
wbCurrent.Worksheets(11).Visible = False
wbCurrent.Worksheets(13).Visible = False
wbCurrent.Worksheets(14).Visible = False
End Sub
Private Sub mnuCarbonTankTable_Click()
On Error Resume Next
'Set xlCurrent = GetObject(, "Excel.Application"
If xlCurrent Is Nothing Then
MsgBox "Excel Instance not found"
Exit Sub
Else
Call openExcel
End If
wbCurrent.Worksheets(9).Visible = True
xlCurrent.Visible = True
'Application.ScreenUpdating = False
wbCurrent.Worksheets(1).Visible = False
wbCurrent.Worksheets(2).Visible = False
wbCurrent.Worksheets(3).Visible = False
wbCurrent.Worksheets(4).Visible = False
wbCurrent.Worksheets(6).Visible = False
wbCurrent.Worksheets(7).Visible = False
wbCurrent.Worksheets(8).Visible = False
wbCurrent.Worksheets(5).Visible = False
wbCurrent.Worksheets(10).Visible = False
wbCurrent.Worksheets(11).Visible = False
wbCurrent.Worksheets(12).Visible = False
wbCurrent.Worksheets(13).Visible = False
wbCurrent.Worksheets(14).Visible = False
End Sub
Private Sub mnuCloseExcel_Click()
If Not xlCurrent Is Nothing Then
xlCurrent.DisplayAlerts = False
xlCurrent.CommandBars.ActiveMenuBar.Enabled = True
xlCurrent.DisplayFullScreen = True
xlCurrent.Quit
DoEvents
Set xlCurrent = Nothing
Set wbCurrent = Nothing
End Sub
please go through my code and check where i am wrong.
Thanks