Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How to Programmatically Save All Open Workbooks


View products that this article applies to.

This article was previously published under Q213416

↑ Back to the top


Summary

In Microsoft Excel you can create a Microsoft Visual Basic for Applications Sub procedure (macro) that automatically saves all open Excel workbooks.

This article describes a macro that automatically saves any open workbook that has been previously saved and named, and also opens a dialog box that prompts the user to name and save any workbooks that have not been saved and named.

↑ Back to the top


More information

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:

Microsoft Certified Partners - https://partner.microsoft.com/global/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS In Microsoft Excel, you can use the AUTOSAVE.XLA add-in macro to save all open documents automatically as you work. If you prefer not to have your documents automatically saved, but would like to save all open documents at one time, you can create and use a sample macro that performs the same action as opening each workbook and then clicking Save on the File menu.

Create the Macro

  1. In Excel, press ALT+F11 to start the visual basic editor.
  2. On the Insert menu, click Module.
  3. In the module sheet, type the following code:
    Sub SaveAll()
        ' Store the Activeworkbook in a variable.
        Set aw = ActiveWorkbook
            For Each wb In Workbooks
                If wb.Path <> "" Then ' Save file if it has been saved
                                      ' previously
                    wb.Save
                Else  ' If not previously saved, activate and show the
                      ' Save As dialog box.
                    wb.Activate
                    Application.Dialogs(xlDialogSaveAs).Show
                End If
            Next
        aw.Activate   ' Activate the original Activeworkbook.
    End Sub
    					
  4. Press ALT+Fll to return to Excel.

Place the Macro in a Menu

If you want to create a menu item to run the macro, follow these steps:
  1. On the View menu, point to Toolbars, and then click Customize.
  2. Click the Commands tab.
  3. In Categories list , click Macros.
  4. Under Commands, drag Custom Menu Item to the File menu.

    NOTE: Wait for the File menu to open and then drop Custom Menu Item into the menu.
  5. On the File menu, Right-click Custom Menu Item.
  6. In the Name box on the shortcut menu, type Save &All.
  7. Click Assign Macro on the shortcut menu.
  8. In the Macro name list, click SaveAll, and then click OK.
  9. In the Customize dialog box, click Close.
To run the macro and save all open workbooks, click Save All on the File menu. You can create this macro and make the menu changes in a new workbook or in an existing workbook. TIP: To make the menu choice always available in Microsoft Excel, store the macro and make the menu changes in a Personal Macro Workbook, and then always open the Personal Macro Workbook when you are using Excel.

↑ Back to the top


References

For more information about customizing menus, click Microsoft Excel Help on the Help menu, type customize menus and toolbars in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

↑ Back to the top


Keywords: KB213416, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213416
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 297