Application.AutomationSecurity 重新启用宏在模板中由宏创建的新文件中运行

问题描述 投票:0回答:1

我有一个使用 Excel VBA 的 Excel 项目报告系统。报告文件每月创建一次,即一月一个文件,二月一个文件,依此类推。

每个月月底,我使用第 1 册中的宏打开模板文件,然后将当月的汇总数据结转到新月份的报告文件(第 2 册)中。

第 1 册宏例程在第 2 册工作表之一上重置日期(到该月的 1 日),这就是我的问题。输入此日期的单元格链接到该日期更改时运行的第 2 册宏。

Application.AutomationSecurity = msoAutomationSecurityForceDisable
在 Book 1 宏开始时停止 Book 2 宏运行。

重置日期后,我使用

Application.AutomationSecurity = PreviousSecurity
,其中 Previous Security 是一个变量,其中包含 AutomationSecurity 被禁用之前的初始状态。

宏顺利结束,并将 Book 2 文件带到前台,此时用户需要手动运行 Book 2 宏,以更新徽标。尽管我已经重置了自动化安全性,但在关闭并重新打开文件之前,我无法运行第 2 册中的宏。

第 1 册宏的相关部分:

Sub NewCarryFwd3()

    Dim Folder As String, CFileName As String
' The 'NewClient' and 'NewProject' variables are defined as Public at head of module

' Get file names
    Folder = ActiveWorkbook.Path
    CFileName = ActiveWorkbook.Name

' Stop Excel screen updating
    Application.ScreenUpdating = False

' Stop autocalculation of formulae to speed up procedure
    Application.Calculation = xlCalculationManual
    
'**********
'Prevent Macros in the new Template running
    Dim previousSecurity As MsoAutomationSecurity
    PreviousSecurity = Application.AutomationSecurity
    Application.AutomationSecurity = msoAutomationSecurityForceDisable

'**********
' Calculate new DRS month dates
    Dim OldDate As Date, NewDate As Date, NewMonth As String
    OldDate = Workbooks(CFileName).Sheets("Setup").Range("Month_start").Value
    NewDate = DateAdd("m", 1, OldDate)
    NewMonth = Choose(Month(NewDate), "Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec")

' Open New DRS File from Microsoft user's template folder
    Dim NextDRS As String
    Workbooks.Add template:= _
        "C:\Users\" & Environ("username") & "\AppData\Roaming\Microsoft\Templates\Oasis 3D DRS_V3.xltm"
    NextDRS = ActiveWorkbook.Name

'******************************************************************************

**' My Code to update the summary data goes in here - too much to include!**

**' After updating Summary data the macro ends as below**

'******************************************************************************

' Set initial Weekly date to first day of new month?
' Set first weekly data
    Workbooks(NextDRS).Sheets("Weekly").Range("WkDay7").Value = NewDate
    Sheets("Weekly").Protect Sheets("Weekly").Protect
    
' Allow macros in New workbook to run
    Application.AutomationSecurity = PreviousSecurity
    
' Create New file Name
    Dim NewFilename As String
    NewFilename = Folder & "\DRS " & Workbooks(CFileName).Sheets("Setup").Range("C2").Text & " " & NewMonth & ".xlsm"
    Workbooks(NextDRS).Activate
    Sheets("Daily Report").Select

' Check if new file name already exists if so add numeric ID
    Dim f As Long
    
    f = 1
    Do While Dir(NewFilename) <> ""
        NewFilename = Folder & "\DRS " & Workbooks(CFileName).Sheets("Setup").Range("C2").Text & " " & NewMonth & " " & f & ".xlsm"
        f = f + 1
    Loop

'**********
' Save new workbook for next month in Excel 2007 xlsm format.

    Workbooks(NextDRS).SaveAs FileName:= _
        NewFilename _
        , FileFormat:=52, Password:="", WriteResPassword:="", _
        ReadOnlyRecommended:=False, CreateBackup:=True

' Re-activate screen updating & autocalculation
    Application.ScreenUpdating = True

End Sub

他们是否有某种方法可以重新启用 AutomationSecurity,以便无需关闭并重新打开文件即可运行第 2 册宏?

excel vba
1个回答
0
投票

您需要关闭打开/添加的工作簿,将 AutomationSecurity 属性重置为以前的值,然后重新打开您的工作簿。

我认为您可能误解了 AutomationSecurity 属性的范围。来自对象参考:

"Returns or sets an MsoAutomationSecurity constant that represents the security mode that Microsoft Excel uses when programmatically opening files."

换句话说,该属性仅影响工作簿的打开方式。它不会更改已打开的工作簿的行为。

我认为你的解决方案就是:

  1. 更新摘要数据后以编程方式关闭工作簿,
  2. 将 AutomationSecurity 属性设置回之前的状态,然后
  3. 以编程方式重新打开工作簿。

此后一切都应该很好,用户可能甚至不知道发生了什么变化。

© www.soinside.com 2019 - 2024. All rights reserved.