强制人们启用 Word 宏

Force people to enable Word macros

我想确保打开我的 Word 文档的每个人都启用宏。我想到了一个文本文件:“请启用宏以查看此文档”,如果确实启用了宏我想用原始内容修改文件以使其成为看起来很正常。

我能在 VBA 中做到这一点吗?

提前致谢,
罗宾

首先,您必须知道,这不能为实验过的 VBA 用户完成...

  1. 当然,您的文档必须是 .docm 类型(启用宏的文档);

  2. 请复制 ThisDocument 代码模块中的下一个代码:

Option Explicit

Const bookMrk As String = "BlockBMrk"


Private Sub Document_Close()
    If ThisDocument.Bookmarks.Exists(bookMrk) = True Then
        With ThisDocument
            .ActiveWindow.View.Type = wdPrintPreview
            If ThisDocument.ProtectionType = -1 Then .Protect wdAllowOnlyFormFields, , "testPass", , True
            .Save
        End With
    Else
        If ThisDocument.ProtectionType = 2 Then ThisDocument.Unprotect "testPass"
        With Selection
            .HomeKey Unit:=wdStory
            .TypeText Text:="Please re-open this file with ""Enable Macros"" selected." & vbCrLf & _
                            "Otherwise, you cannot see its content..." & vbCrLf & vbCrLf & _
                            " To be able to enable macros go File -> Options -> Trust Center -> " & _
                            "Trust Center Settings... -> Macro Settings and check 'Disable all macros with notification' and press OK."
            
            .InsertBreak Type:=wdPageBreak
            .HomeKey Unit:=wdStory, Extend:=wdExtend
            ThisDocument.Bookmarks.Add Name:=bookMrk, _
            Range:=Selection.Range
        End With
        With ThisDocument
            .ActiveWindow.View.Type = wdPrintPreview
            .Protect wdAllowOnlyFormFields, Password:="testPass"
            .Save
        End With
    End If
End Sub

Private Sub Document_Open()
    MacrosEnabled
End Sub

Sub MacrosEnabled()
    ThisDocument.Unprotect Password:="testPass"
    ThisDocument.Bookmarks(bookMrk).Select
    Selection.Delete
End Sub

2之二。保护(以某种方式)VBA 代码不被看到:

   a. Right click on the document Project (being in VBE);
   b. Choose 'Protection' tab;
   c. Check 'Lock project for viewing';
   d. Set a password (keep it in your mind, if need to modify something...);
   e. Confirm the password and press 'OK'

但是你要知道破解这个密码并不是很复杂(对于有经验的人来说)。

  1. 在文档中写点东西(任何你想要的),保存文档并关闭它。

  2. 在启用或不启用宏的情况下打开它,看看发生了什么...

请测试以上解决方案并发送一些反馈。