View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
alexia_net alexia_net is offline
external usenet poster
 
Posts: 1
Default How to kill excel from visual basic

Hi
I am using the followig code. The thing is that excel won't stop. It is
something related to For...Next.
If I remove For...Next then everything is ok. What could be wrong?


On Error GoTo ErrorHandler

Dim MyXLApp As New Excel.Application
Application.DisplayAlerts = False
Dim strSheetName As String
strSheetName = "INFORMATION"
MyXLApp.Workbooks.Open FileName:="C:\yyy\1_08A.xls"



If MyXLApp.Worksheets.Count = 1 Then
MyXLApp.Worksheets.Add.Name = "DELETE"
MyXLApp.Workbooks.Application.SaveWorkspace
End If

If MyXLApp.Worksheets.Count 1 Then
For buc_sheets = 1 To MyXLApp.Worksheets.Count
den_sheet = Worksheets(buc_sheets).Name
If strSheetName = den_sheet Then
MyXLApp.Worksheets(den_sheet) Then
MyXLApp.Worksheets(strSheetName).Activate
MyXLApp.Worksheets(strSheetName).Delete
End If
Next
End If
GoTo line100
MyXLApp.Workbooks.Application.SaveWorkspace
line100:
MyXLApp.Workbooks.Application.SaveWorkspace
MyXLApp.Workbooks.Close
Application.DisplayAlerts = True
MyXLApp.Application.Quit
Set MyXLApp = Nothing
ErrorHandler:
Select Case Err.Number
Case 9
MsgBox Err.Description
End Select