Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

closing excel instances using vb- urgent 1

Status
Not open for further replies.

852456

Programmer
Jun 26, 2003
2
0
0
CA
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
 
This will do the trick:

Sub CloseAllInstancesOfExcel()

Dim objXL As Excel.Application
On Error Resume Next

Do
Set objXL = GetObject(, "Excel.Application")
With objXL
.DisplayAlerts = False
.Quit
End With
DoEvents
Set objXL = Nothing
Loop Until Err <> 0

End Sub
 
Hi
thanks for ur reply but this code is not working. I want to close worksheets of a same excel workbook which r opened through different submenus of a form through some code.
thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top