Firstly, you must know that this cannot be done for experimented VBA users...
Your document must be of a type .docm
(Macro - Enabled Document), of course;
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).
Write something in the document (whatever you want), Save the document and close it.
Open it with or without Macro Enalbled and see what's happening...
Please, test the above solution and send some feedback.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…