簡體   English   中英

檢查用戶是否啟用了宏

[英]Check if the user has macros enabled

我只需要在禁用宏時顯示特定工作表。

我留下或多或少的照片和參考資料,我找到了要做的事情。

在此處輸入圖像描述

如果宏被禁用,您將無法運行任何代碼,它們被禁用=)

可以做的是默認顯示一些“禁用宏”工作表,並確保工作簿始終在該工作表處於活動狀態的情況下保存。

然后在Workbook.Open事件處理程序( Private Sub Workbook_Open in ThisWorkbook )中,隱藏“宏被禁用”表,並使其他表可見。

像這樣的東西(僅用於概念 - 這是未經測試的空氣代碼),其中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

如果您需要工作簿在禁用宏的情況下仍然可用,那么請記住,保存工作簿時的 state 將是它打開的 state - 無論宏是否啟用 - 所以因為你不知道是否接下來將在啟用或禁用宏的情況下打開工作簿,您必須准備工作簿以在保存時處於“禁用宏”state 中。

Excel 將其設置保留在 Windows 注冊表中。 Disable all with notification | Disable all except digitally signed macros | Disable all without notification | Enable all macros (not recommended) ,一旦應用,將被轉換為值2 | 3 | 4 | 1VBAWarnings下的DWORD值VBAWarnings:

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

其中 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

您可以使用 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