K
Kurt Remlin
Hi,
I wrote that simple program to get an idea how to work with Excel
objects from VB.
It should (and actually does) create a new Excel workbook with three
worksheets named "1", "2" and "3" (in this order from left to right)
and save it.
Please look at the code below and let me know if this is a good way to
do it. Is there a way (and need) to streamline the code?
TIA
'============================================
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim i As Integer
Dim strFileName As String
Dim bAlerts As Boolean
On Error GoTo Quit
Set xlApp = New Excel.Application
xlApp.Visible = False
xlApp.SheetsInNewWorkbook = 1
Set xlWB = xlApp.Workbooks.Add
For i = 1 To 3
Set xlWS = xlWB.Worksheets.Add(After:=xlWB.Worksheets(i))
xlWS.Name = Str$(i)
Next i
bAlerts = xlApp.DisplayAlerts
xlApp.DisplayAlerts = False
xlWB.Worksheets("Sheet1").Delete
xlWB.Worksheets(1).Activate
' strFileName = xlApp.GetSaveAsFilename
strFileName = "C:\Temp\Temp.xls"
xlWB.Close SaveChanges:=True, FileName:=strFileName
xlApp.DisplayAlerts = bAlerts
Quit:
xlApp.Quit
Set xlWS = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
'===================================================
I wrote that simple program to get an idea how to work with Excel
objects from VB.
It should (and actually does) create a new Excel workbook with three
worksheets named "1", "2" and "3" (in this order from left to right)
and save it.
Please look at the code below and let me know if this is a good way to
do it. Is there a way (and need) to streamline the code?
TIA
'============================================
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim i As Integer
Dim strFileName As String
Dim bAlerts As Boolean
On Error GoTo Quit
Set xlApp = New Excel.Application
xlApp.Visible = False
xlApp.SheetsInNewWorkbook = 1
Set xlWB = xlApp.Workbooks.Add
For i = 1 To 3
Set xlWS = xlWB.Worksheets.Add(After:=xlWB.Worksheets(i))
xlWS.Name = Str$(i)
Next i
bAlerts = xlApp.DisplayAlerts
xlApp.DisplayAlerts = False
xlWB.Worksheets("Sheet1").Delete
xlWB.Worksheets(1).Activate
' strFileName = xlApp.GetSaveAsFilename
strFileName = "C:\Temp\Temp.xls"
xlWB.Close SaveChanges:=True, FileName:=strFileName
xlApp.DisplayAlerts = bAlerts
Quit:
xlApp.Quit
Set xlWS = Nothing
Set xlWB = Nothing
Set xlApp = Nothing
'===================================================