Microsoft KB Archive/213416

= XL2000: How to Programmatically Save All Open Workbooks =

Article ID: 213416

Article Last Modified on 11/23/2006

-

APPLIES TO


 * Microsoft Excel 2000 Standard Edition

-



This article was previously published under Q213416





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.



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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site:

https://partner.microsoft.com/global/30000104

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
 In Excel, press ALT+F11 to start the visual basic editor. On the Insert menu, click Module.  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  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.
 * 1) On the File menu, Right-click Custom Menu Item.
 * 2) In the Name box on the shortcut menu, type Save &All.
 * 3) Click Assign Macro on the shortcut menu.
 * 4) In the Macro name list, click SaveAll, and then click OK.
 * 5) 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.

