Hi
I am trying to create 134 Excel Macro Enable workbooks from my Access application.
My code is
The code creates the first file but fails at line "xlApp.ActiveWorkbook.SaveAs FileName:=strFileName, FileFormat:=52" when it loops to the second record.
The error message is "Run time Error 91, Object Variable or With block variable not set"
Could someone please tell me where I am going wrong with this?
Many thanks
I am trying to create 134 Excel Macro Enable workbooks from my Access application.
My code is
Code:
Public Function CreateDataFiles()
Dim xlApp As Object
Dim xlWB As Object
Dim strFileName As String
Set xlWB = Workbooks.Add("C:\KPMG\Excel Templates\Member Firm DataFile.xlsm")
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT * FROM qryDataFileDetails")
If Not (rs.EOF And rs.BOF) Then
rs.MoveFirst
Do Until rs.EOF = True
strFileName = "C:\KPMG\TestingDataFiles\" & rs!FileName
Set xlApp = CreateObject("Excel.Application")
xlApp.ActiveWorkbook.SaveAs FileName:=strFileName, FileFormat:=52
ActiveWorkbook.Close
'Move to the next record. Don't ever forget to do this.
rs.MoveNext
Loop
End If
rs.Close
Set rs = Nothing
xlApp.Quit
Set xlApp = Nothing
xlWB.Close True
End Function
The code creates the first file but fails at line "xlApp.ActiveWorkbook.SaveAs FileName:=strFileName, FileFormat:=52" when it loops to the second record.
The error message is "Run time Error 91, Object Variable or With block variable not set"
Could someone please tell me where I am going wrong with this?
Many thanks