简体   繁体   English

检查用户是否启用了宏

[英]Check if the user has macros enabled

I only need to show a specific sheet when macros are disabled.我只需要在禁用宏时显示特定工作表。

I leave photos and references of more or less what I locate to do.我留下或多或少的照片和参考资料,我找到了要做的事情。

在此处输入图像描述

You can't run any code if macros are disabled, they're disabled =)如果宏被禁用,您将无法运行任何代码,它们被禁用=)

What you can do, is show some "macros are disabled" sheet by default, and make sure the workbook is always saved with that sheet active.可以做的是默认显示一些“禁用宏”工作表,并确保工作簿始终在该工作表处于活动状态的情况下保存。

Then in the Workbook.Open event handler ( Private Sub Workbook_Open in ThisWorkbook ), hide the "macros are disabled" sheet, and make the other ones visible.然后在Workbook.Open事件处理程序( Private Sub Workbook_Open in ThisWorkbook )中,隐藏“宏被禁用”表,并使其他表可见。

Something like this (just for the concept - this is untested air-code), where MacrosDisabledSheet is the code name of your "macros are disabled, please enable macros to use this workbook" sheet - this should hide all sheets except the "macros are disabled" sheet on save, and then unhide all sheets (and hide the "macros are disabled" sheet) once macros are enabled: or when the workbook is opened with macros enabled:像这样的东西(仅用于概念 - 这是未经测试的空气代码),其中MacrosDisabledSheet是“宏被禁用,请启用宏以使用此工作簿”表的代号- 这应该隐藏除“宏是保存时禁用”工作表,然后在启用宏后取消隐藏所有工作表(并隐藏“禁用宏”工作表):或在启用宏的情况下打开工作簿时:

Private Sub Workbook_Open()
'when opened with macros enabled, runs on open.
'when opened with macros disabled, runs when macros are enabled.
    ShowAllWorksheets
    ShowMacrosDisabledSheet hide:=True
End Sub

Private Sub Workbook_BeforeSave()
'always save with the "macros disabled" sheet active/visible
    ShowAllWorksheets hide:=True
    ShowMacrosDisabledSheet
End Sub

Private Sub ShowAllWorksheets(Optional ByVal hide As Boolean = False)
    Dim sheet As Worksheet
    For Each sheet In ThisWorkbook.Worksheets
        If Not sheet Is MacrosDisabledSheet Then
            If hide Then
                If sheet.Visible <> xlSheetHidden Then sheet.Visible = xlSheetHidden
            Else 
                If sheet.Visible <> xlSheetVisible Then sheet.Visible = xlSheetVisible
            End If
        End If
    Next
End Sub

Private Sub ShowMacrosDisabledSheet(Optional ByVal hide As Boolean = False)
    MacrosDisabledSheet.Visible = IIf(hide, xlSheetVeryHidden, xlSheetVisible)
End Sub

If you need the workbook to still be usable with macros disabled, then just keep in mind that the state of the workbook when you save it, will be the state it opens in - whether macros are enabled or not - so because you cannot know if the workbook will next be opened with macros enabled or disabled, you have to prepare the workbook to be in a "macros are disabled" state on save.如果您需要工作簿在禁用宏的情况下仍然可用,那么请记住,保存工作簿时的 state 将是它打开的 state - 无论宏是否启用 - 所以因为你不知道是否接下来将在启用或禁用宏的情况下打开工作簿,您必须准备工作簿以在保存时处于“禁用宏”state 中。

Excel keeps its settings in the Windows registry. Excel 将其设置保留在 Windows 注册表中。 Disable all with notification | Disable all with notification | Disable all except digitally signed macros | Disable all except digitally signed macros | Disable all without notification | Disable all without notification | Enable all macros (not recommended) , which once applied, will be translated into the values 2 | Enable all macros (not recommended) ,一旦应用,将被转换为值2 | 3 | 3 | 4 | 4 | 1 for the DWORD value VBAWarnings under the hive: 1VBAWarnings下的DWORD值VBAWarnings:

HKEY_CURRENT_USER\software\policies\microsoft\office\15.0\excel\security

where 15.0 stands for the Excel version installed on the system.其中 15.0 代表系统上安装的 Excel 版本。


Value 1: Enable All macros
Value 2: Disable all macros with notification
Value 3: Disable all macros except those digitally signed
Value 4: Disable all without notification

You can use the following code in VBA to read and write the Windows registry keys:您可以使用 VBA 中的以下代码来读取和写入 Windows 注册表项:

'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function

'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created

' change REG_DWORD to the correct key type
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String = "REG_DWORD")
Dim myWS As Object

  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub

'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function

ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function

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

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