In this post, we are delighted to provide you with 2 quick and useful approaches to prevent shared Word template files from being changed.
Within a company or business organization, it’s common to upload shared files on a public folder, including some Word documents, so everyone can access it. Sometimes, these template files require no change. However, it happens when some people accidentally modify and save the template. Therefore, you might have to keep an eye for this situation and replace the changed template with the original one all the time.
With no doubt, this work requires a lot time and efforts. So, we offer you the macro way to do the task.
Method 1: Prevent a Specific Word Template File from Being Changed
This macro will find and check if a template file has been changed according to its last modified time. If the time of template does not match with that of its original copy, then it will be replaced by the latter.
- First, open Word and press “Alt+ F11” to open VBA editor.
- Next, in “Normal” project, click “Insert” tab on the menu bar.
- Then choose “Module” on the drop-down menu.
- Open the editing area on the module by double click.
- And paste following codes there:
Sub CheckAndReplaceTheModifiedFile() Dim strSharedFile As String Dim strSharedFilePath As String Dim strSharedFileName As String Dim strOriginalFile As String ' Change the path as the actual path of your original template file. strOriginalFile = "C:\test\Doc1.docx" ' Change the path as the actual path of the shared file which is going to be checked. strSharedFile = "C:\Users\shared folder\Doc1.docx" strSharedFilePath = Left(strSharedFile, InStrRev(strSharedFile, "\")) strSharedFileName = Right(strSharedFile, Len(strSharedFile) - InStrRev(strSharedFile, "\")) If FileDateTime(strSharedFile) <> FileDateTime(strOriginalFile) Then nReturnValue = MsgBox("The file: " & strSharedFileName & " in share folder has been modified, do you want to replace it with the original file?", 4) If nReturnValue = 6 Then Kill strSharedFile FileCopy strOriginalFile, strSharedFilePath & strSharedFileName MsgBox ("The file: " & strSharedFileName & " has been replaced with original file") End If End If End Sub
- Now you need to make modifications to this macro.
- First replace the file path in following code line with the actual address of your unchanged original file:
strOriginalFile = "C:\Users\Public\Documents\Sample\Test 1\DWORDR.docx"
- Second, replace the path in following line with an actual one of your template file in public folder:
strSharedFile = "C:\Users\Public\Documents\Sample\Share Folder\DWORDR.docx"
- After this, click “Run” or hit “F5”.
- Then there is the confirmation box, asking if you want to replace the changed file. Click “Yes” or “No” accordingly.
- And when the replacement is done, there is the result box:
Method 2: Prevent Multiple Templates from Being Changed
- First of all, insert a 2-column table with multiple rows in a new document. In the first column, enter the paths of templates in public folder. And in the second column, enter addresses of unchanged files.
- Then follow above steps to install and run a macro.
- Remember to replace macro with this one:
Sub CheckAndReplaceMultipleModifiedFiles() Dim objTable As Table Dim objSharedFile As Cell Dim objSharedFileRange As Range Dim objOriginalFileRange As Range Dim nRowNumber As Integer Dim strSharedFile As String Dim strOriginalFile As String Set objTable = ActiveDocument.Tables(1) nRowNumber = 1 For Each objSharedFile In objTable.Columns(1).Cells Set objSharedFileRange = objSharedFile.Range objSharedFileRange.MoveEnd Unit:=wdCharacter, Count:=-1 Set objOriginalFileRange = objTable.Cell(nRowNumber, 2).Range objOriginalFileRange.MoveEnd Unit:=wdCharacter, Count:=-1 If objSharedFileRange.Text <> "" Then strSharedFile = objSharedFileRange.Text strOriginalFile = objOriginalFileRange.Text Call CheckAndReplaceTheModifiedFileInTableList(strSharedFile, strOriginalFile) End If nRowNumber = nRowNumber + 1 Next End Sub Sub CheckAndReplaceTheModifiedFileInTableList(strSharedFile, strOriginalFile) Dim strSharedFilePath As String Dim strSharedFileName As String strSharedFilePath = Left(strSharedFile, InStrRev(strSharedFile, "\")) strSharedFileName = Right(strSharedFile, Len(strSharedFile) - InStrRev(strSharedFile, "\")) If FileDateTime(strSharedFile) <> FileDateTime(strOriginalFile) Then Kill strSharedFile FileCopy strOriginalFile, strSharedFilePath & strSharedFileName MsgBox ("The file: " & strSharedFileName & " has been replaced with original file") End If End Sub
- If there are files being replaced, you will get message box as shown in method 1.
Check Your Backups Often
Backups can be a great help in times of data disaster. Certainly, we mean valid backups. There are times you resort to backups but only to find it’s damaged somehow. Therefore, you have to take time to check if your backups are sound and intact. Otherwise, you will need a tool to repair docx when data loss occurs.
Author Introduction:
Vera Chen is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including damaged xls and pdf repair software products. For more information visit www.datanumen.com
Leave a Reply