强制Excel用户启用macros

如果用户没有启用macros,是否有防止打开Excel工作簿的方法?

这样做的标准方法是强制用户使用启animation面启用macros。

还可以通过写入registry来修改VBA设置(例如,使用VBS – 尽pipe在公司设置中,GPO可能会阻止此操作)。 有关示例,请参阅http://blogs.msdn.com/b/cristib/archive/2012/02/29/vba-programmatically-enable-access-to-the-vba-object-model-using-macros.aspx 。访问registry。

初始屏幕方法

  • 除了启animation面外,工作簿中的所有工作表都非常隐蔽 (只能通过VBA或VBA编辑器进行更改)
  • 如果macros被启用:
    1)打开工作簿时,代码将取消隐藏所有这些非常隐藏的工作
    2)当工作簿closures时,所有这些表都被重新隐藏起来
  • 如果没有启用macros,用户只能看到启animation面“请启用macros,closures然后重新打开这个文件”

下面列出了这个技术的完整代码的两个链接

  1. TekTips的Brad Yundt

代码进入ThisWorkbook模块

 Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean) Dim ws As Worksheet, wsSplash As Worksheet Application.ScreenUpdating = False Application.EnableEvents = False Set wsSplash = Worksheets("Splash screen") wsSplash.Visible = xlSheetVisible For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Splash screen" Then ws.Visible = xlSheetVeryHidden Next ws Cancel = True ThisWorkbook.Save For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Splash screen" Then ws.Visible = xlSheetVisible Next ws wsSplash.Visible = xlSheetVeryHidden Application.EnableEvents = True Application.ScreenUpdating = True End Sub Private Sub Workbook_Open() Dim ws As Worksheet, wsSplash As Worksheet Dim Pswd As String Pswd="myPassword" Application.ScreenUpdating = False Set wsSplash = Worksheets("Splash screen") wsSplash.Visible = xlSheetVisible For Each ws In ThisWorkbook.Worksheets If ws.Name <> "Splash screen" Then If ws.Name="Sheet1" Then If InputBox("Please enter your password")=Pswd Then ws.Visible=xlSheetVisible Else ws.Visible = xlSheetVisible End If End If Next ws wsSplash.Visible = xlSheetVeryHidden Application.ScreenUpdating = True End Sub 
  1. Jonske 在VBAeXpress

代码进入ThisWorkbook模块

 Option Explicit Private Sub Workbook_Open() With Application 'disable the ESC key .EnableCancelKey = xlDisabled .ScreenUpdating = False Call UnhideSheets .ScreenUpdating = True 're-enable ESC key .EnableCancelKey = xlInterrupt End With End Sub ' Private Sub UnhideSheets() ' Dim Sheet As Object ' For Each Sheet In Sheets If Not Sheet.Name = "Prompt" Then Sheet.Visible = xlSheetVisible End If Next ' Sheets("Prompt").Visible = xlSheetVeryHidden ' Application.Goto Worksheets(1).[A1], True '< Optional ' Set Sheet = Nothing ActiveWorkbook.Saved = True End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) With Application .EnableCancelKey = xlDisabled .ScreenUpdating = False Call HideSheets .ScreenUpdating = True .EnableCancelKey = xlInterrupt End With End Sub Private Sub HideSheets() ' Dim Sheet As Object '< Includes worksheets and chartsheets ' With Sheets("Prompt") ' 'the hiding of the sheets constitutes a change that generates 'an automatic "Save?" prompt, so IF the book has already 'been saved prior to this point, the next line and the lines 'relating to .[A100] below bypass the "Save?" dialog... If ThisWorkbook.Saved = True Then .[A100] = "Saved" ' .Visible = xlSheetVisible ' For Each Sheet In Sheets If Not Sheet.Name = "Prompt" Then Sheet.Visible = xlSheetVeryHidden End If Next ' If .[A100] = "Saved" Then .[A100].ClearContents ThisWorkbook.Save End If ' Set Sheet = Nothing End With ' End Sub