Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
333 views
in Technique[技术] by (71.8m points)

vba - Force people to enable Word macros

I want to make sure that everyone who opens my Word document enables macros. I've thought of a document with the text: "Please enable macros to view this document", and if macros are actually enabled I want to modify the file with the original content to make it seem normal.

Is there anyway I can do this in VBA?

Thanks in advance,
Robin

question from:https://stackoverflow.com/questions/66062623/force-people-to-enable-word-macros

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Firstly, you must know that this cannot be done for experimented VBA users...

  1. Your document must be of a type .docm (Macro - Enabled Document), of course;

  2. Please, copy the next code in ThisDocument code module:

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 bis. Protect (somehow) the VBA code to not be seen:

   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'

But you must know that it is not very complicated to break this password (for an experienced person).

  1. Write something in the document (whatever you want), Save the document and close it.

  2. Open it with or without Macro Enalbled and see what's happening...

Please, test the above solution and send some feedback.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...