In today’s post, we will give you 2 ways to protect your privacy by auto deleting personal information from Word document via VBA.
More or less, the document we are working consists of some personal information which might expose your privacy, such as follows:
Therefore, you need to take actions against such privacy leak, such as deleting document properties as to remove sensitive data.
In one of our previous articles, we’ve shown you the steps to manually remove personal information from document. And this time, we offer you 2 macro methods to achieve the same task in a quick and dirty manner.
Method 1: Auto Delete Personal Information while Closing a Document
- Firstly, press “Alt+ F11” in Word to invoke VBA editor.
- Next, create a new module by clicking “Normal”.
- Then click “Insert” and select “Module” on the drop-down menu.
- Now double click to open the module.
- Paste the following codes:
Sub AutoClose() Dim strButtonValue As String Dim strFile As String Dim objDoc As Document Set objDoc = ActiveDocument If objDoc.Path <> "" Then strButtonValue = MsgBox("Do you want to remove the document properties of this document?", vbYesNo) If strButtonValue = vbYes Then objDoc.RemoveDocumentInformation (wdRDIDocumentProperties) Else Exit Sub End If objDoc.Save End If End Sub
- Next, click “Save”.
- Once you close a document, you will trigger a box, asking if you want to remove the document properties of this document. Click “Yes” or “No” accordingly.
- If you click “Yes”, the macro will automatically remove all document properties and save the document and then close it. If you choose “No”, the document will be closed immediately.
Method 2: Auto Delete Personal Information from Multiple Documents
- To begin with, arrange all target documents under one directory.
- Next open Word and follow above steps to install macro.
- Then paste these codes:
Sub RemoveDocPropertiesOfMultiDoc() Dim strButtonValue As String Dim strFile As String Dim objDoc As Document Dim StrFolder As String Dim dlgFile As FileDialog Set dlgFile = Application.FileDialog(msoFileDialogFolderPicker) With dlgFile If .Show = -1 Then StrFolder = .SelectedItems(1) & "\" Else MsgBox "No folder is selected! Please select the target folder." Exit Sub End If End With strFile = Dir(StrFolder & "*.docx", vbNormal) While strFile <> "" Set objDoc = Documents.Open(FileName:=StrFolder & strFile) objDoc.RemoveDocumentInformation (wdRDIDocumentProperties) objDoc.Save objDoc.Close strFile = Dir() Wend End Sub
- Click “Run” or press “F5” to get codes executed.
- Now in the “Browse” window open, select the folder where you keep documents.
- Lastly, click “OK” and let Word do its job.
Cope with Word Errors
A simple user error can bring down Word, and leave documents damaged. If don’t act effectively, there is a high chance to lose all data. So, it’s advised to obtain a preeminent tool to repair doc as soon as possible.
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including corrupt Excel and pdf repair software products. For more information visit www.datanumen.com