XL2000: OnSave Property Is Not Available in Object Browser (213779)



The information in this article applies to:

  • Microsoft Excel 2000

This article was previously published under Q213779

SUMMARY

In the Visual Basic Editor for Microsoft Excel 2000, there is no reference to the OnSave property in the Object Browser.

NOTE: This behavior differs from the functionality of the Object Browser used in versions of Microsoft Excel earlier than Excel 97.

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: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site: The OnSave property has been replaced by the BeforeSave event in Excel 97 and Excel 2000.

The OnSave property was introduced in Excel for Windows 95, version 7.0, but can still be used in Excel 2000. The OnSave property returns or sets the name of a Visual Basic procedure to run after the user runs either the Save or Save As command, but before the workbook is actually saved. Note the following considerations before you use the OnSave property:
  • The procedure that is specified to run with the OnSave property must take one Boolean argument (see the following example).
  • Only Visual Basic procedures are supported by this property; Excel 4.0 Macro Language procedures are not supported.
  • The value of the OnSave property is not saved with the workbook; it must be reset each time the workbook is opened.
  • This event is not called if the workbook is saved when a macro or mail command runs, or when an embedded workbook is updated.

Macro Example Using the OnSave Property

This example displays a message box after the user runs either the Save or Save As command, but before the workbook is actually saved:
'Specifies the procedure to run when the workbook is saved
Sub SetSaveEvent()
    ActiveWorkbook.OnSave = "SaveProcedure"
End Sub

Sub SaveProcedure(s As Boolean)
    MsgBox "Microsoft Excel will now save your work."
End Sub
				
Run the SetSaveEvent macro. This sets an internal flag in Excel to run the SaveProcedure macro automatically when the active workbook is saved.

Macro Example Using the BeforeSave Event

The BeforeSave event runs before a workbook is saved.

This example prompts the user for either a yes or no response before saving the workbook:
  1. In the Project Explorer window of the Visual Basic Editor, double-click ThisWorkbook in the current project.

    This opens a module for code that runs behind the workbook.
  2. In the Object list of this module, click Workbook.
  3. In the Procedure list of this module, click BeforeSave.
  4. For the Workbook_BeforeSave procedure, type or paste the following code:
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, _
        Cancel As Boolean)
    
        a = MsgBox("Do you really want to save this workbook?", vbYesNo)
        If a = vbNo Then Cancel = True
    
    End Sub
    					
  5. On the File menu, click Close and Return to Microsoft Excel.
  6. On the File menu, click Save.

    A message box appears with a prompt asking if you really want to save the file. If you click Yes, your file is saved. If you click No, your file is not saved.

REFERENCES

For more information about the BeforeSave event, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type beforesave event in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbinfo kbProgramming KB213779