I have some code to open an excel workbook in the background, run the macro in that workbook, delete a worksheet, then save as.
It all works perfect apart from the delete worksheet part. Can anyone help?
This is my code:
Public Sub RunExcelMacroOrSub()
'declare variables
Dim xlApp As Object
Dim xlBook As Object
'excel application stuff
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:/temp/book2.xls")
'run the macro
xlApp.Run "Macro1"
'delete Sheet1
xlApp.Sheets("Sheet1").Select
xlApp.ActiveWindow.SelectedSheets.Delete
xlApp.Range("A1").Select
'save file
xlApp.displayalerts = False
xlBook.Saveas ("c:/temp/hopeitworked.xls")
xlApp.displayalerts = True
'done
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
It all works perfect apart from the delete worksheet part. Can anyone help?
This is my code:
Public Sub RunExcelMacroOrSub()
'declare variables
Dim xlApp As Object
Dim xlBook As Object
'excel application stuff
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Open("C:/temp/book2.xls")
'run the macro
xlApp.Run "Macro1"
'delete Sheet1
xlApp.Sheets("Sheet1").Select
xlApp.ActiveWindow.SelectedSheets.Delete
xlApp.Range("A1").Select
'save file
xlApp.displayalerts = False
xlBook.Saveas ("c:/temp/hopeitworked.xls")
xlApp.displayalerts = True
'done
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub