简体   繁体   English

检查Excel宏安全设置

[英]Check Excel Macro Security Settings

I have a vb.net Application that starts a Excel macro. 我有一个启动Excel宏的vb.net应用程序。 Is there a way to check the macro Security Settings of the Excel first? 有没有一种方法可以先检查Excel的宏安全设置?

Imports Excel = Microsoft.Office.Interop.Excel
Private Sub macrotest ()

     objExcel.Workbooks.Open(strPath)

     if ***'Check her what macro Security Settings is selected!*** Then
         objExcel.Run("SortIO")
     end if
End Sub

I found on this site that you can check the registry for the security settings. 我在站点上发现可以检查注册表的安全设置。 The one issue I found was that my registry key was not in the exact place that was referenced on the site. 我发现的一个问题是我的注册表项不在站点上所引用的确切位置。 That being said, you may want to just Try Catch it or make a more elaborate method for finding the VBAWarnings key in the registry. 话虽这么说,您可能只想尝试捕获它,或者使用更复杂的方法在注册表中查找VBAWarnings项。

    Dim strVBASecuritySetting As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Microsoft\Office\15.0\Excel\Security", _
                                                                        "VBAWarnings", String.Empty)

    If strVBASecuritySetting = "1" Then

        objExcel.Run("SortIO")

    End If

These are the possible values for VBAWarnings: 这些是VBAWarnings的可能值:

  1. Enable All 全部启用
  2. Disable All with Notification 禁用所有通知功能
  3. Disable All Except Digitally Signed 禁用除数字签名以外的所有内容
  4. Disable All without Notification 全部禁用,恕不另行通知

This seems to have cross-posted to the Excel VBA questions page: would you mind speaking a little more slowly? 这似乎已经交叉发布到Excel VBA问题页面:您介意说慢一点吗?

... reads slowly ... ... 读起来很慢 ...

Well, I can give you an answer in VBA: the Excel.Application.AutomationSecurity property. 好吧,我可以在VBA中给您一个答案: Excel.Application.AutomationSecurity属性。

The available values are enumerated by Office.MsoAutomationSecurity: 可用值由Office.MsoAutomationSecurity枚举:

1 msoAutomationSecurityLow 1 msoAutomationSecurityLow
2 msoAutomationSecurityByUI 2个msoAutomationSecurityByUI
3 msoAutomationSecurityForceDisable 3 msoAutomationSecurityForceDisable

These are the settings for macros available in the API exposed to VBA. 这些是公开给VBA的API中可用的宏的设置。 The user interface, visible in the 'Developer' tab of the ribbon, corresponds to the registry settings explained by NoAlias in the previous answer: 功能区的“开发人员”选项卡中可见的用户界面与NoAlias在上一个答案中解释的注册表设置相对应:

HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\15.0\\Excel\\Security\\VBAWarnings HKEY_CURRENT_USER \\ Software \\ Microsoft \\ Office \\ 15.0 \\ Excel \\ Security \\ VBA警告

With the following enumerated values: 具有以下枚举值:

  1. Enable all macros 启用所有宏
  2. Disable all macros with notification 禁用所有带有通知的宏
  3. Disable all macros except digitally signed macros 禁用所有宏,除了数字签名的宏
  4. Disable all macros without notification 禁用所有宏,恕不另行通知

If you go down the registry route, a VBA coder's advice would be to use the safe registry classes exposed by WMI scripting... And check that your user does actually have the privileges to make that registry write: there may be a registry policy in place preventing it. 如果您沿注册表路线走,VBA编码器的建议是使用WMI脚本公开的安全注册表类...并检查您的用户是否确实具有使该注册表写入的特权:可能存在一个注册表策略防止它的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM