Below is the code where template file File1.xlsm is loaded with new data and saved it as File2.xlsm and close the File1.xlsm without saving it. But I am having two problems:
1. Once I try to open the File2.xlsm after database finish the process, it gives the error:
"Excel cannot open the file '01-06-2016 File2.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"
2. File1.xlsm does not get closed without saving the content and asked the user to save/unsave the file before closing.
I am not sure where i am doing wrong. Please give me some suggestions.
1. Once I try to open the File2.xlsm after database finish the process, it gives the error:
"Excel cannot open the file '01-06-2016 File2.xlsm' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file"
2. File1.xlsm does not get closed without saving the content and asked the user to save/unsave the file before closing.
I am not sure where i am doing wrong. Please give me some suggestions.
Code:
Dim objApp1 As Object
Dim objBook1 As Object
Dim objSheet1 As Object
Dim strSQL1 as string
Dim db As DAO.Database
Dim rst1 as DAO.recordset
Set objApp1 = CreateObject("Excel.Application")
Set objBook1 = objApp1.Workbooks.Open("C:\Desktop\File1.xlsm")
'Set objApp1 = objBook1.Parent
'Set objSheet1 = objBook1.Worksheets("Data Input")
Set objSheet1 = objBook1.ActiveSheet
objBook1.Application.Visible = True
'Opens the recordset
strSQL1 = "Select * from sometable"
Set rst1 = CurrentDb.OpenRecordset(strSQL1)
With ActiveSheet
'Clears the current contents in the workbook range
.Range("A5:G65000").ClearContents
'rst Copies the recordset into the worksheet
.Range("A5").CopyFromRecordset rst1
End With
rst1.Close
objBook1.SaveAs "C\Desktop\" & Format(Now(), "mm-dd-yyyy") & " File2.xlsm ", FileFormat:=xlOpenXMLWorkbookMacroEnabled
objBook1.Close
Set rst1 = Nothing
Set objSheet1 = Nothing
Set objBook1 = Nothing
Set objApp1 = Nothing