P
Pendragon
Access03/WinXP
I am using the following to export a recordset to Excel; this works without
issue. The problem is that the workbook is not being saved as directed in
the code - the Excel window still shows "BookX" where X is the index for the
most recently created workbook.
I'm not using a Close or Quit function because after clicking a button to
create the export, it's necessary to direct the user to the Excel spreadsheet
for further data manipulation. I would like to have the filed saved
programmatically with a pre-defined file name.
All suggestions appreciated.
With objExcel
.Visible = True
.Workbooks.Add
.ActiveSheet.Name = CStr("CustomerInfo")
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 1) = "'" & rs.Fields(i).Name
Next i
.Range(.Cells(1, 1), .Cells(1, rs.Fields.Count)).Font.Bold = True
.Selection.Range("A2").CopyFromRecordset rs
.Range(.Cells(1, 1), .Cells(rs.RecordCount + 1,
rs.Fields.Count)).Columns.AutoFit
.Workbooks(1).SaveAs stDocName
End With
BTW, is there a way to programmatically retrieve the workbook index from
"BookX"? In other instances where I have used a Close command, e.g.,
..Workbooks(1).Close False, , False, it seems like the wrong workbook gets
closed. In creating a new workbook, isn't that given index #1 before all
other open workbooks?
I am using the following to export a recordset to Excel; this works without
issue. The problem is that the workbook is not being saved as directed in
the code - the Excel window still shows "BookX" where X is the index for the
most recently created workbook.
I'm not using a Close or Quit function because after clicking a button to
create the export, it's necessary to direct the user to the Excel spreadsheet
for further data manipulation. I would like to have the filed saved
programmatically with a pre-defined file name.
All suggestions appreciated.
With objExcel
.Visible = True
.Workbooks.Add
.ActiveSheet.Name = CStr("CustomerInfo")
For i = 0 To rs.Fields.Count - 1
.Cells(1, i + 1) = "'" & rs.Fields(i).Name
Next i
.Range(.Cells(1, 1), .Cells(1, rs.Fields.Count)).Font.Bold = True
.Selection.Range("A2").CopyFromRecordset rs
.Range(.Cells(1, 1), .Cells(rs.RecordCount + 1,
rs.Fields.Count)).Columns.AutoFit
.Workbooks(1).SaveAs stDocName
End With
BTW, is there a way to programmatically retrieve the workbook index from
"BookX"? In other instances where I have used a Close command, e.g.,
..Workbooks(1).Close False, , False, it seems like the wrong workbook gets
closed. In creating a new workbook, isn't that given index #1 before all
other open workbooks?