Disable all dialog boxes in Excel while running VBscript?

后端 未结 5 1552
旧巷少年郎
旧巷少年郎 2021-01-04 01:19

I have some code in VB that saves all XLSM files as XLSX. I already have the code that will do that for me, but dialog boxes show up for every action. This was fine for a fe

5条回答
  •  心在旅途
    2021-01-04 02:03

    In order to get around the Enable Macro prompt I suggest

    Application.AutomationSecurity = msoAutomationSecurityForceDisable
    

    Be sure to return it to default when you are done

    Application.AutomationSecurity = msoAutomationSecurityLow
    

    A reminder that the .SaveAs function contains all optional arguments.I recommend removing CreatBackup:= False as it is not necessary.

    The most interesting way I think is to create an object of the workbook and access the .SaveAs property that way. I have not tested it but you are never using Workbooks.Open rendering Application.AutomationSecurity inapplicable. Possibly saving resources and time as well.

    That said I was able to execute the following without any notifications on Excel 2013 windows 10.

        Option Explicit
    
        Sub Convert()
    
        OptimizeVBA (True)  
        'function to set all the things you want to set, but hate keying in
    
        Application.AutomationSecurity = msoAutomationSecurityForceDisable  
        'this should stop those pesky enable prompts
    
        ChDir "F:\VBA Macros\Stack Overflow Questions\When changing type xlsm to 
        xlsx stop popup"
    
        Workbooks.Open ("Book1.xlsm")
    
        ActiveWorkbook.SaveAs Filename:= _
        "F:\VBA Macros\Stack Overflow Questions\When changing type xlsm to xlsx_ 
        stop popup\Book1.xlsx" _
        , FileFormat:=xlOpenXMLWorkbook
    
        ActiveWorkbook.Close
    
        Application.AutomationSecurity = msoAutomationSecurityLow 
        'make sure you set this up when done
    
        Kill ("F:\VBA Macros\Stack Overflow Questions\When changing type xlsm_ 
        to xlsx stop popup\Book1.xlsx") 'clean up
    
        OptimizeVBA (False)
        End Sub
    
    
        Function OptimizeVBA(ByRef Status As Boolean)
    
        If Status = True Then
            Application.ScreenUpdating = False
            Application.Calculation = xlCalculationManual
            Application.DisplayAlerts = False
            Application.EnableEvents = False
        Else
            Application.ScreenUpdating = True
            Application.Calculation = xlCalculationAutomatic
            Application.DisplayAlerts = True
            Application.EnableEvents = True
        End If
    
        End Function
    

提交回复
热议问题