Still learning Ms Access vba but learning fast.
I am not understanding fully and I am stumped on just on two things of which I am sure solve one, the other will also be solved. Spent the last 2 days with no progress.
I have a button on my form opening my excel spreadsheet (template), inserting my recordset from a table into Sheet1 and this is all working fine. My code is below
My problem is when I go to save and close I get the error:
Runtime Error 91:
Object variable or with block variable not set.
I thought I have set everything, but I do not understand what else I need 'set'? I am totally confused.
Can anybody show me what to do - Thank you in advance
My Code:
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Dim rs As Recordset
Set xlapp = New Excel.Application
MySheetPath = "z:\Template.xltx"
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets(1)
Set rs = CurrentDb.OpenRecordset("tbl_MyTable")
XlSheet.Range("A6").CopyFromRecordset rs
Set rs = Nothing
'Insert Row and the Value in the excel sheet starting at specified cell This is where I would put Date
XlSheet.Rows(2).EntireRow.Insert
XlSheet.Range("A3") = "ABC"
Xl.ActiveWorkbook.SaveAs FileName:="z:\Test.xlsx", FileFormat:=51 '<<<<<< Error 91 on this line and next line
Xl.ActiveWorkbook.Close
'Clean up and end with worksheet visible on the screen
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing
I am not understanding fully and I am stumped on just on two things of which I am sure solve one, the other will also be solved. Spent the last 2 days with no progress.
I have a button on my form opening my excel spreadsheet (template), inserting my recordset from a table into Sheet1 and this is all working fine. My code is below
My problem is when I go to save and close I get the error:
Runtime Error 91:
Object variable or with block variable not set.
I thought I have set everything, but I do not understand what else I need 'set'? I am totally confused.
Can anybody show me what to do - Thank you in advance
My Code:
Dim MySheetPath As String
Dim Xl As Excel.Application
Dim XlBook As Excel.Workbook
Dim XlSheet As Excel.Worksheet
Dim rs As Recordset
Set xlapp = New Excel.Application
MySheetPath = "z:\Template.xltx"
Set Xl = CreateObject("Excel.Application")
Set XlBook = GetObject(MySheetPath)
Xl.Visible = True
XlBook.Windows(1).Visible = True
Set XlSheet = XlBook.Worksheets(1)
Set rs = CurrentDb.OpenRecordset("tbl_MyTable")
XlSheet.Range("A6").CopyFromRecordset rs
Set rs = Nothing
'Insert Row and the Value in the excel sheet starting at specified cell This is where I would put Date
XlSheet.Rows(2).EntireRow.Insert
XlSheet.Range("A3") = "ABC"
Xl.ActiveWorkbook.SaveAs FileName:="z:\Test.xlsx", FileFormat:=51 '<<<<<< Error 91 on this line and next line
Xl.ActiveWorkbook.Close
'Clean up and end with worksheet visible on the screen
Set Xl = Nothing
Set XlBook = Nothing
Set XlSheet = Nothing