Search code examples
excelvbams-access

Application.DisplayAlerts = False in Excel object created in Access


This is a macro to update a report. This file would need to overwrite the existing file.

With Application.DisplayAlerts = False I still get the pop up saying this file exists and if I want to replace.

Is there something wrong with my code or is there a method to automatically click yes?

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

Solution

  • Application.DisplayAlerts = False
    

    refers to the application where your code is running, not the Excel instance you created.

    xl.DisplayAlerts = False
    

    would be what you want.