How to: Save Workbooks
Some code examples in this topic use the this or Me keyword or the
You can save a workbook using themethod. If the workbook has not been saved before, you should call the method instead, so you can specify a path. Without an explicit path, Microsoft Office Excel 2003 saves the file in the current folder with the name it was given when it was created. Use the method to save without modifying the open workbook in memory.
To save all open workbooks
Iterate through thecollection and call the method on each workbook.
The SaveAs method is more complex than the Save method. This method allows you to save the specified workbook to a new location or with a new name, optionally specifying a file format, a password, an access mode, and more. Place the following code in the
To save the workbook in XML format
Save the workbook associated with the project to a specified location, saving in XML format:
You might want to set theproperty to False before calling the SaveAs method, because saving in some formats requires interaction. Setting this property to False causes Excel to use all defaults.
The SaveCopyAs method saves a copy of the workbook to a file but does not modify the open workbook in memory. This method is useful when you want to create a backup copy without modifying the location of the workbook.
To save a backup copy of a workbook
Compiling the Code
This code must be placed in the
ThisWorkbookclass, not in a sheet class.
Interactively canceling any of the methods that save or copy the workbook raises a run-time error in your code. For example, if your procedure calls the SaveAs method but does not disable prompts from Excel, and your user clicks Cancel when prompted, Excel raises a run-time error.