简体   繁体   中英

Check Excel Macro Security Settings

I have a vb.net Application that starts a Excel macro. Is there a way to check the macro Security Settings of the Excel first?

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.

    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:

  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?

... reads slowly ...

Well, I can give you an answer in VBA: the Excel.Application.AutomationSecurity property.

The available values are enumerated by Office.MsoAutomationSecurity:

1 msoAutomationSecurityLow
2 msoAutomationSecurityByUI
3 msoAutomationSecurityForceDisable

These are the settings for macros available in the API exposed to VBA. The user interface, visible in the 'Developer' tab of the ribbon, corresponds to the registry settings explained by NoAlias in the previous answer:

HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\15.0\\Excel\\Security\\VBAWarnings

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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