In this article, we will talk about the way to use macro to quickly protect a batch of Word documents with password as required.
Nowadays, data breach is becoming a more and more worrying concerns for many people, especially netizens. For example, identity theft can result in a huge loss of money and damage to someone’s reputation. Therefore, it’s time to take precautionary measures to prevent ourselves from falling victim to data leakage.
It is out of this very need that we decide to arm you with the way to mass protect your Word files.
Prevent Multiple Documents from Opening
For some files with high confidence, you probably want no one else to view them at all. Then you can take the following instruction to set a password to protect them.
- First off, place all files need to be encrypted under the same directory.
- Click “Developer” tab.
- Then click “Visual Basic” in “Code” group. Or you can press “Alt+ F11” to open the VBA editor.
- Next insert a new module under “Normal” project by clicking “Normal” first on the left column.
- And then click “Insert” tab on the menu bar.
- Next choose “Module” on the drop-down menu.
- Now double click on the new module to have the coding space.
- Paste the following codes:
Sub ProtectMultiDocWithOpenPassword() Dim objDoc As Document Dim strPassword As String, strFile As String, strFolder As String strPassword = "123" strFolder = "E:\Temp\test\" strFile = Dir(strFolder & "*.docx", vbNormal) While strFile <> "" Set objDoc = Documents.Open(FileName:=strFolder & strFile) With objDoc .Password = strPassword .SaveAs2 FileName:=objDoc.FullName, Password:=strPassword .Close End With strFile = Dir() Wend End Sub
- Finally, click “Run” or hit “F5”.
Notes:
- In code line “strPassword = “123””, remember to replace the “123” with your own password.
- In code line “strFolder = “C:\Users\Public\Documents\New folder\””, change the file folder path to an actual one.
From now onwards, anyone who wants to see the protected file will have to enter the correct password.
Prevent Multiple Documents from Editing
Here is another situation when files are fine for public review but should be prevented from editing.
Use the exact way to install a macro as shown above, only to replace the macro with the bellowing one:
Sub ProtectMultiDocWithEditPassword() Dim objDoc As Document Dim strPassword As String, strFile As String, strFolder As String Set objDoc = ActiveDocument strPassword = "456" strFolder = "C:\Users\Public\Documents\New folder\" strFile = Dir(strFolder & "*.docx", vbNormal) While strFile <> "" Set objDoc = Documents.Open(FileName:=strFolder & strFile) objDoc.Protect Password:=strPassword, NoReset:=False, Type:= _ wdAllowOnlyReading, UseIRM:=False, EnforceStyleLock:=False objDoc.Save objDoc.Close strFile = Dir() Wend End Sub
Mind the File Corruption
Data leakage can also compromised one’s file and crash it. However, to recover doc is not easy. Sometimes you file might get erased or hijacked. Under this circumstance, you can resort to a third-party repairing tool for help.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including tool to fix xls and pdf repair software products. For more information visit www.datanumen.com