Microsoft KB Archive/898511

= When you programmatically save a workbook in Excel, a menu command does not run in the BeforeSave event =

Article ID: 898511

Article Last Modified on 1/20/2007

-

APPLIES TO


 * Microsoft Office Excel 2007
 * Microsoft Office Excel 2003
 * Microsoft Excel 2002 Standard Edition
 * Microsoft Excel 2000 Standard Edition

-



SYMPTOMS
You programmatically save a workbook in Microsoft Excel. If you try to perform a menu command in an event, such as the BeforeSave event, the menu command does not run.



CAUSE
This issue occurs because a menu command cannot be nested in the code when you programmatically call a menu command such as the Save command. The menu command in the BeforeSave event is specifically blocked from running.



WORKAROUND
To work around this issue, manually save the workbook. To manually save the workbook, use one of the following procedures, as appropriate for the version of Excel that you are running:
 * In Microsoft Office Excel 2007, click the Microsoft Office Button, and then click Save.
 * In Microsoft Office 2003 and in earlier versions of Excel, click Save on the File menu.

When you use this workaround, you can perform any of the following menu commands in the BeforeSave event:
 * Any command on the Format menu.
 * The Clear command on the Edit menu.
 * The Delete command on the Edit menu.
 * The Delete rows command on the Edit menu.
 * The Print command on the File menu.



MORE INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements.

To reproduce this issue, follow these steps, as appropriate for the version of Excel that you are running.

Excel 2007
 Open a new workbook in Excel. Click the Microsoft Office Button, click Save As, click Excel Macro-Enabled Workbook (*.xlsm) in the Save as type box, type a file name in the File name box, and then click Save. Type some text in cell A1, and then press ENTER. Click the Developer tab. If the Developer tab is not displayed, follow these steps:  Click the Microsoft Office Button, and then click Excel Options. Click Popular.</li> Click to select the Show Developer tab in the Ribbon check box.</li> Click OK to close the Excel Options dialog box.</li></ol> </li> Click Visual Basic in the Code group to start the Visual Basic Editor.</li> In the Project - VBAProject pane, double-click ThisWorkbook.</li> Follow these steps: <ol style="list-style-type: lower-alpha;"> In the Object box, click Workbook.</li> In the Procedure box, click BeforeSave.</li>  Add the following macro code to the BeforeSave event: Range(&quot;a1&quot;).ClearContents </li>  Under the macro code that you typed in step 7c, type the following macro code: Public Sub Test

ThisWorkbook.Save

End Sub </li></ol> </li> In the workbook, click the Microsoft Office Button, and then click Save.

The text in cell A1 is cleared.</li> Click Cancel to close the Save As dialog box.</li> Type some text in cell A1, and then press ENTER.</li> On the Developer tab, click Macros in the Code group.</li> Click the ThisWorkbook.Test macro, and then click Run.

The text in cell A1 is not cleared when the workbook is saved.</li></ol>

Excel 2003 and earlier versions of Excel
<ol> Open a new workbook in Excel.</li> Type some text in cell A1, and then press ENTER.</li> <li>On the Tools menu, point to Macro, and then click Visual Basic Editor.</li> <li>In the Project - VBAProject pane, double-click ThisWorkbook.</li> <li>Follow these steps: <ol style="list-style-type: lower-alpha;"> <li>In the Object box, click Workbook.</li> <li>In the Procedure box, click BeforeSave.</li> <li> Add the following macro code to the BeforeSave event: Range(&quot;a1&quot;).ClearContents </li> <li> Under the macro code that you typed in step 5c, type the following macro code: Public Sub Test

ThisWorkbook.Save

End Sub </li></ol> </li> <li>In the workbook, click Save on the File menu.

The text in cell A1 is cleared.</li> <li>Click Cancel to close the Save As dialog box.</li> <li>Type some text in cell A1, and then press ENTER.</li> <li>On the Tools menu, point to Macro, and then click Macros.</li> <li>Click the ThisWorkbook.Test macro, and then click Run.

The text in cell A1 is not cleared when the workbook is saved.</li></ol>

Additional query words: excel2007 excel2k7 excel12 xl2007 xl2k7 xl12 excel2003 excel2002 excel2000 xl2003 xl2002 xl2000 xl2k3 xl2k2 xl2k

Keywords: kbexpertiseinter kbmacro kbvba kbprogramming kbautomation kbtshoot kbprb KB898511

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.