davis1118
11-10-2017, 04:34 PM
First off I am not experienced with coding so please go easy with my question and code below because I'm sure there will be much wrong with it. I have more experience with using VBA in Excel and just started "playing" around with Word VBA, so I'm not very familiar with the coding differences. I am using Microsoft Word 2013.
(Everything I am trying to do below I am currently doing with Excel with success, but I cannot figure out the code with Word.
I am trying to create a backup copy of the Word document when I save the file. I want the code to create a subfolder "Archive" in the same directory as the original document, and then save the backup copy in the "Archive" subfolder with the current date in the title. My intent is to have one "master" document that can be linked to, and then the history of changes in the archive folder.
Here is the code I am using.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim folderPath As String
Dim myName As String
Dim ext As String
Dim backupdirectory As String
folderPath = Application.ActiveDocument.Path
myName = Left(ActiveDocument.Name, (InStrRev(ActiveDocument.Name, ".") - 1))
ext = Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStrRev(ActiveDocument.Name, "."))
backupdirectory = "Archive"
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(ActiveDocument.Path & "/" & backupdirectory) Then
fso.CreateFolder (ActiveDocument.Path & "/" & backupdirectory)
End If
Dim T As String
T = Format(Now, "ddMmmyy hh mm ss")
ActiveDocument.SaveAs FileName:=folderPath & "\" & backupdirectory & "\" & myName & "_" & T & "." & ext
End Sub
This code is partially working. It creates the "Archive" subfolder and saves the document correctly the first time it's saved. But when the document is saved a second time it creates another subfolder in the first subfolder. In that second subfolder the file name is the first backup file name combined with the second backup name. I am not sure why this is happening. Any help would be appreciated.
Thank you very much.
(Everything I am trying to do below I am currently doing with Excel with success, but I cannot figure out the code with Word.
I am trying to create a backup copy of the Word document when I save the file. I want the code to create a subfolder "Archive" in the same directory as the original document, and then save the backup copy in the "Archive" subfolder with the current date in the title. My intent is to have one "master" document that can be linked to, and then the history of changes in the archive folder.
Here is the code I am using.
Private Sub App_DocumentBeforeSave(ByVal Doc As Document, SaveAsUI As Boolean, Cancel As Boolean)
Dim folderPath As String
Dim myName As String
Dim ext As String
Dim backupdirectory As String
folderPath = Application.ActiveDocument.Path
myName = Left(ActiveDocument.Name, (InStrRev(ActiveDocument.Name, ".") - 1))
ext = Right(ActiveDocument.Name, Len(ActiveDocument.Name) - InStrRev(ActiveDocument.Name, "."))
backupdirectory = "Archive"
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(ActiveDocument.Path & "/" & backupdirectory) Then
fso.CreateFolder (ActiveDocument.Path & "/" & backupdirectory)
End If
Dim T As String
T = Format(Now, "ddMmmyy hh mm ss")
ActiveDocument.SaveAs FileName:=folderPath & "\" & backupdirectory & "\" & myName & "_" & T & "." & ext
End Sub
This code is partially working. It creates the "Archive" subfolder and saves the document correctly the first time it's saved. But when the document is saved a second time it creates another subfolder in the first subfolder. In that second subfolder the file name is the first backup file name combined with the second backup name. I am not sure why this is happening. Any help would be appreciated.
Thank you very much.