Share via

Visual Basic Command to Save All Open Documents

Anonymous
2022-09-10T08:47:09+00:00

I was previously told on this help forum (https://answers.microsoft.com/en-us/msoffice/forum/all/autosaving-a-document-to-a-hard-disk-at-a/d9b7cea5-4b33-4b18-b082-5ecfd2380ddd) that the command ActiveDocument.Save can be used to save a currently open document. (This command corresponds to the Ctrl + S key shortcut.)

Does anyone know what Visual Basic command can be used to send the save command to all open documents, and not just to the one which is active?

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

9 answers

Sort by: Most helpful
  1. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2022-09-10T08:58:05+00:00

    Sub Test()
    Dim D As Document
    For Each D In Documents
    D.Save
    Next
    End Sub

    If you only want to save open documents (not unsaved newly created documents):

    Sub Test()
    Dim D As Document
    For Each D In Documents
    If D.Path <> "" Then D.Save
    Next
    End Sub

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Jay Freedman 207.7K Reputation points Volunteer Moderator
    2022-09-10T20:13:29+00:00

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2022-09-10T16:11:28+00:00

    There's no need for a delay, today's systems are all multitasking and therefore can save many files at the same time.

    There is also another reason why I need to learn how to insert a delay command in Visual Basic. If I want to show a short message for 100 milliseconds when the save command is executed, what commands can be used to show the message for the specified number of milliseconds?

    For example, the message “SAVING” is to be shown for 100 milliseconds.

    Was this answer helpful?

    0 comments No comments
  4. Andreas Killer 144.1K Reputation points Volunteer Moderator
    2022-09-10T11:39:00+00:00

    Do you also know how to add a 10-second delay command, so that all open documents may not be saved at the same moment (which may perhaps create some errors due to writing much data at the same time)

    There's no need for a delay, today's systems are all multitasking and therefore can save many files at the same time.

    Andreas.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2022-09-10T10:59:33+00:00

    Sub Test()
    Dim D As Document
    For Each D In Documents
    D.Save
    Next
    End Sub

    If you only want to save open documents (not unsaved newly created documents):

    Sub Test()
    Dim D As Document
    For Each D In Documents
    If D.Path <> "" Then D.Save
    Next
    End Sub

    Thank you.

    I have tried to edit the script, adding the statement If D.Saved = False, which checks whether a document is already saved:

    Dim D As Document

    For Each D In Documents

    If (D.Saved = False) Or (D.Path <> "") Then D.Save

    Next

    Do you also know how to add a 10-second delay command, so that all open documents may not be saved at the same moment (which may perhaps create some errors due to writing much data at the same time) but that each of the open documents may be saved after 10 seconds?

    Was this answer helpful?

    0 comments No comments