2021年3月14日星期日

VBA - error when trying to save file - Object doesn't support property or method?

I'm trying to run this code from a separate Excel file but I keep getting an on the xl.SaveAs line stating that the Object doesn't support the property or method. When I reference "xl." in the other sections such as the Refresh Table section, it works perfectly but not on the SaveAs method. What am I doing wrong?

Sub DailyRefresh ()      'Open and refresh Access      Dim appAccess As Object      Set appAccess = GetObject("S:\Shared\DailyRefresh.accdb")      Application.DisplayAlerts = False      appAccess.Visible = True      appAccess.DoCmd.RunMacro "Run_All_Queries"      appAccess.CloseCurrentDatabase       'Open Excel        Set xl = CreateObject("Excel.Application")      xl.Workbooks.Open ("s:\Shared\Template.xlsx")      xl.Visible = True      Application.DisplayAlerts = False        'Set date to the 1st of the Month on Summary tab      xl.Sheets("Summary").Visible = True      xl.Sheets("Summary").Select      xl.Range("C10").Value = DateSerial(Year(Now), Month(Now), 1)      xl.Range("C10").NumberFormat = "mm/dd/yyyy"    ' REFRESH Table      xl.Sheets("Data").Visible = True      xl.Sheets("Data").Select      xl.Range("A1").Select      xl.Range("DailyRefresh.accdb[[#Headers],[ACTIVITY_DT]]").Select      xl.Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False      xl.Worksheets("Fname").Visible = True      xl.Sheets("Fname").Select               'Copy and Save AS            Application.DisplayAlerts = False      Path = "S:\Shared\NewTemplate"      Filename = xl.Sheets("Fname").Range("A7").Value      xl.SaveAs Path & Filename & ".xlsx", FileFormat:=51, CreateBackup:=False      xl.Worksheets("Fname").Visible = False      xl.Close      Application.DisplayAlerts = True        End Sub  
https://stackoverflow.com/questions/66631836/vba-error-when-trying-to-save-file-object-doesnt-support-property-or-method March 15, 2021 at 11:00AM

没有评论:

发表评论