运行VB脚本时禁用Excel中的所有对话框?

我在VB中有一些代码,将所有的XLSM文件保存为XLSX。 我已经有了这样的代码,但是对于每一个操作,都会显示对话框。 这对于几十个文件来说很好。 但是,我将立刻在数百个XLSM文件上使用这个function,而且我不能一天到晚在单击我的电脑时单击对话框。

我试过的代码非常多:

Application.DisplayAlerts = False 

虽然这不会导致错误,但它也不起作用。

这些框给出了有关启用macros的警告,并警告说XLSX会删除所有macros的文件。 考虑到警告的types,我怀疑是由于安全风险限制closures这些对话框。

由于我在Excel的VB编辑器中运行这个代码,有没有可能让我禁用debugging对话框的选项?

我也试过:

 Application.DisplayAlerts = False Application.EnableEvents = False ' applied code Application.DisableAlerts = True Application.EnableEvents = True 

这些都没有工作。

编辑:

以下是我目前代码中的代码:

 Public Sub example() Application.DisplayAlerts = False Application.EnableEvents = False For Each element In sArray XLSMToXLSX(element) Next element Application.DisplayAlerts = False Application.EnableEvents = False End Sub Sub XLSMToXLSX(ByVal file As String) Do While WorkFile <> "" If Right(WorkFile, 4) <> "xlsx" Then Workbooks.Open Filename:=myPath & WorkFile Application.DisplayAlerts = False Application.EnableEvents = False ActiveWorkbook.SaveAs Filename:= _ modifiedFileName, FileFormat:= _ xlOpenXMLWorkbook, CreateBackup:=False Application.DisplayAlerts = True Application.EnableEvents = True ActiveWorkbook.Close End If WorkFile = Dir() Loop End Sub 

我也包围了For循环,而不是ActiveWorkbook.SaveAs行:

 Public Sub example() For Each element In sArray XLSMToXLSX(element) Next element End Sub 

最后,我将Application.DisplayAlerts移到Workbooks.Open行的上方:

 Sub XLSMToXLSX(ByVal file As String) Do While WorkFile <> "" If Right(WorkFile, 4) <> "xlsx" Then Workbooks.Open Filename:=myPath & WorkFile Application.DisplayAlerts = False Application.EnableEvents = False ActiveWorkbook.SaveAs Filename:= _ modifiedFileName, FileFormat:= _ xlOpenXMLWorkbook, CreateBackup:=False Application.DisplayAlerts = True Application.EnableEvents = True ActiveWorkbook.Close End If WorkFile = Dir() Loop End Sub 

这些工作都没有。

编辑:

如果有帮助,我正在使用Excel for Mac 2011。

从Excelmacros安全 – http://www.excelfunctions.net

Excel 2007,2010和2013中的macros安全性:

…..

由最新版本的Excel提供的不同的Excel文件types使工作簿包含macros时清楚,因此这本身是一个有用的安全措施。 但是,Excel也具有可选的macros安全设置,通过选项菜单进行控制。 这些是 :

'禁用所有的macros而不通知'

  • 此设置不允许任何macros运行。 当您打开一个新的Excel工作簿时,不会提醒您包含macros,因此您可能不知道这是工作簿无法正常工作的原因。

'使用通知禁用所有macros'

  • 此设置可防止macros运行。 但是,如果工作簿中有macros,则会显示一个popup窗口,以警告您存在并已禁用macros。

'禁用除数字签名macros之外的所有macros'

  • 此设置仅允许来自可信来源的macros运行。 所有其他macros不运行。 当您打开一个新的Excel工作簿时,不会提醒您包含macros,因此您可能不知道这是工作簿无法正常工作的原因。

“启用所有macros”

  • 这个设置允许所有的macros运行。 当您打开一个新的Excel工作簿时,不会提醒您包含macros,并且可能不知道在打开文件时运行的macros。

如果您信任macros并且可以启用它们,请select此选项:

“启用所有macros”

而这个对话框不应该显示为macros。

至于保存对话框,注意到这是在Excel 2011 for Mac上运行后,我遇到了以下问题SO, StackOverflow – 使用VBA保存包含Excel文件(.xlsm)的macros作为非macros时的抑制对话框包含文件(.xlsx) 。 从中,除了可能通过一些键盘input模拟,除去对话似乎是不可能的。 我会张贴另一个问题来询问这个问题。 对不起,我只能半途而废。 另一种select是使用带有Microsoft Excel的Windows计算机,但是在这种情况下我不确定这是否是一个选项。

解决scheme:自动化macros

这听起来像你会从使用自动化工具中受益。 如果你正在使用Windows PC,我会推荐AutoHotkey 。 我没有在Mac上使用自动化实用程序,但这个询问不同的post有几个build议,但没有一个似乎是免费的。

这不是一个VBA解决scheme。 这些macros在Excel之外运行,可以使用键盘笔画,鼠标移动和点击与程序进行交互。

基本上,您可以录制或编写一个简单的自动化macros,等待Excel“另存为”对话框变为活动状态,点击进入/返回以完成保存操作,然后等待“另存为”窗口closures。 您可以将其设置为连续循环运行,直到您手动结束macros为止。

这里有一个简单版本的Windows AutoHotkey脚本,它可以完成你在Mac上试图做的事情。 它应该给你一个有关逻辑的概念。

自动化macros示例:AutoHotkey

 ; ' Infinite loop. End the macro by closing the program from the Windows taskbar. Loop { ; ' Wait for ANY "Save As" dialogue box in any program. ; ' BE CAREFUL! ; ' Ignore the "Confirm Save As" dialogue if attempt is made ; ' to overwrite an existing file. WinWait, Save As,,, Confirm Save As IfWinNotActive, Save As,,, Confirm Save As WinActivate, Save As,,, Confirm Save As WinWaitActive, Save As,,, Confirm Save As sleep, 250 ; ' 0.25 second delay Send, {ENTER} ; ' Save the Excel file. ; ' Wait for the "Save As" dialogue box to close. WinWaitClose, Save As,,, Confirm Save As } 

为了解决启用macros提示,我build议

 Application.AutomationSecurity = msoAutomationSecurityForceDisable 

完成后一定要将其恢复为默认值

 Application.AutomationSecurity = msoAutomationSecurityLow 

提示.SaveAs函数包含所有可选参数。我build议删除CreatBackup:= False因为它不是必需的。

我认为最有趣的方式是创build工作簿的对象,并以这种方式访问.SaveAs属性。 我没有testing过,但你从来没有使用Workbooks.Open呈现Application.AutomationSecurity不适用。 可能节省资源和时间。

这就是说,我可以在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