0
votes

What I would like to achieve is to save a Word document in two places using the Save function. It has two shortcomings, though. First, Word blinks when I save a file (because it closes saves a copy, and it becomes active document, which is closed, and then Word return to the original document). Second, macro add my backup to recent files, despite AddToRecentFiles:=False How to avoid two faults?

Sub FileSave()
Dim currFile As String
Dim backup As String
backup = "C:\Users\" & Environ("UserName") & "\Documents\WordBackups\"
With ActiveDocument
If .Saved Or .Path = "" Then Exit Sub
If Dir(backup, vbDirectory) = "" Then
    MkDir backup
    MsgBox "Backup folder has been created.", vbInformation
End If

    .Bookmarks.Add Name:="LastPosition"
    Application.ScreenUpdating = False
    .Save
    currFile = .FullName
    .SaveAs2 FileName:=backup & .Name, AddToRecentFiles:=False

End With

    ActiveDocument.Close
    Documents.Open FileName:=currFile
    Selection.GoTo What:=wdGoToBookmark, Name:="LastPosition"

End Sub

1
Reading and understanding the code would be a good start, maybe? Seriously, you copy code off the Internet and run it (!) without having the slightest clue how it does what it does? This is extremely... unwise.Tomalak
First of all, you should remove the line On Error Result Next as that will hide any errors - and it could be important to know what errors are generated!Cindy Meister
@Cindy Those are 18 lines of perfectly obvious code, at least to anyone putting in a few minutes of their time to attempt to understand them. The OP apparently did not make such an attempt. Explaining what this code is doing is an exercise in futility.Tomalak
Yes, that thought did cross my mind. OTOH this is supposed to be Q&A, meaning the explanation could help others, coming later. So I compromised and provided "homework" rather than giving the corrected code in my answer... :-)Cindy Meister
@Tomalak Reading carefully is always a good start if one wants to comment on somebody's post. If you had done it, you would have known that I wrote "macro saves a current document when Word closes" (!). I have no idea how you could arrive at conclusion that I did read nor tried to understand the code.menteith

1 Answers

1
votes

This code checks whether the document has changes that need saving. If there are none, the back-up is made. Otherwise, if the document has a file path (has been saved before) the document is saved and the code restarts, then the back-up will be made. Otherwise, the Save As dialog box is shown so that the document can be saved to a path, then the back-up is made.

The tricky part is how the macro should differentiate between "I've only opened and looked at it" vs. "the document has been saved, now make a back-up".

One possibility would be to check for the presence of a document Variable or document Property, set by the Save action and removed by SaveAs. Try this:

  1. File/Info; from the "Properties" dropdown choose "Advanced Properties".
  2. Create a Custom property named MakeBackup as a "Yes/No" type and set it to No.
  3. After the line .Save: ActiveDocument.CustomDocumentProperties("MakeBackup").Value = True
  4. Add a check to the If for the backup to check whether this is true: If .Saved And ActiveDocument.CustomDocumentProperties("MakeBackup").Value = True Then
  5. And after the SaveAs2 line change it back to false: ActiveDocument.CustomDocumentProperties("MakeBackup").Value = False