menteith
01-17-2016, 10:17 AM
Hi all,
I am very new to VBA so I would like to get some help with a macro I have created (some parts of it were found in the Internet). The macro should save make a backup (in a specified path) of a file that is currently being saved, a procedure that is often discussed since Word lacks of it. I will talk over what I think the macro does and should do.
1. FileSave procedure is intercepted.
2. The macro checks if an active document is saved. If it is, no extra action is required and the macro closes.
3. If an active document in not saved, the usual "Save As" dialog appears. If the user chooses to not save the file then the macro closes.
4. If document is not saved, the macros saves it.
5. The macro looks for a backup folder. If it is not found, the macro creates it and shows a message box.
6. Then the macro checks if the source folder is the same as the backup folder. If they are the same, the macro shows a message and closes.
7. The active (current) document is copied to the backup folder. If it fails, a message box is displayed.
I tested this macro and it seems to work. However, there is a huge imperfection. When document is modified and the user chooses to close it, a dialog appears asking if the document should be saved. If the user chooses to do so, the document is saved, but a backup copy is not created - and I would like to create a backup in this situation.
Could you check the macro against errors and suggest how to implement the above-mentioned feature? I am newbie so I am pretty sure that the macro is, to say the least, imperfect and it could be rewrirtten. I did my best to make it as elegant (e.g. I tried to avoid the GoTo procudure) and short as I could.
Please find the macro below.
Sub FileSave()
Dim source As String
Dim DocName As String
Dim objF As Object
Dim retVal As Long
backup = "C:\Users\" & Environ("UserName") & "\Documents\BackupWord\"
With ActiveDocument
If .Saved Then Exit Sub
If .Path = "" Then
If Application.Dialogs(wdDialogFileSaveAs).Show <> -1 Then Exit Sub
End If
'GoTo continue
If Not .Saved Then
ActiveDocument.Save
End If
'continue:
If Dir(backup, vbDirectory) = "" Then
MkDir backup
MsgBox "Backup folder has been created.", vbInformation
End If
source = .Path & "\"
DocName = .Name
If source = backup Then
MsgBox "WARNING! Backup folder is the same as the source folder."
Exit Sub
End If
Set objF = CreateObject("Scripting.FileSystemObject")
retVal = -1
On Error Resume Next
retVal = objF.CopyFile(source & DocName, _
backup & DocName, True)
On Error GoTo 0
Set objF = Nothing
If retVal <> 0 Then
MsgBox "Backup has not been copied to folder " & backup
End If
End With
End Sub
I am very new to VBA so I would like to get some help with a macro I have created (some parts of it were found in the Internet). The macro should save make a backup (in a specified path) of a file that is currently being saved, a procedure that is often discussed since Word lacks of it. I will talk over what I think the macro does and should do.
1. FileSave procedure is intercepted.
2. The macro checks if an active document is saved. If it is, no extra action is required and the macro closes.
3. If an active document in not saved, the usual "Save As" dialog appears. If the user chooses to not save the file then the macro closes.
4. If document is not saved, the macros saves it.
5. The macro looks for a backup folder. If it is not found, the macro creates it and shows a message box.
6. Then the macro checks if the source folder is the same as the backup folder. If they are the same, the macro shows a message and closes.
7. The active (current) document is copied to the backup folder. If it fails, a message box is displayed.
I tested this macro and it seems to work. However, there is a huge imperfection. When document is modified and the user chooses to close it, a dialog appears asking if the document should be saved. If the user chooses to do so, the document is saved, but a backup copy is not created - and I would like to create a backup in this situation.
Could you check the macro against errors and suggest how to implement the above-mentioned feature? I am newbie so I am pretty sure that the macro is, to say the least, imperfect and it could be rewrirtten. I did my best to make it as elegant (e.g. I tried to avoid the GoTo procudure) and short as I could.
Please find the macro below.
Sub FileSave()
Dim source As String
Dim DocName As String
Dim objF As Object
Dim retVal As Long
backup = "C:\Users\" & Environ("UserName") & "\Documents\BackupWord\"
With ActiveDocument
If .Saved Then Exit Sub
If .Path = "" Then
If Application.Dialogs(wdDialogFileSaveAs).Show <> -1 Then Exit Sub
End If
'GoTo continue
If Not .Saved Then
ActiveDocument.Save
End If
'continue:
If Dir(backup, vbDirectory) = "" Then
MkDir backup
MsgBox "Backup folder has been created.", vbInformation
End If
source = .Path & "\"
DocName = .Name
If source = backup Then
MsgBox "WARNING! Backup folder is the same as the source folder."
Exit Sub
End If
Set objF = CreateObject("Scripting.FileSystemObject")
retVal = -1
On Error Resume Next
retVal = objF.CopyFile(source & DocName, _
backup & DocName, True)
On Error GoTo 0
Set objF = Nothing
If retVal <> 0 Then
MsgBox "Backup has not been copied to folder " & backup
End If
End With
End Sub