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.
Sample Code
To add the saved document file name and path to the title
bar of the active document, follow these steps:
- Save and close any open workbooks, and then create a new workbook.
- Start the Visual Basic Editor (press ALT+F11).
- On the Insert menu, click Module.
- Type the following code in the module:
Sub SaveAser()
Application.Dialogs(xlDialogSaveAs).Show
Application.ActiveWindow.Caption = ActiveWorkbook.FullName
End Sub
Sub Saver()
'Checks to see if the file has been saved by looking
'for a colon in the FullName, which includes any path.
If InStr(1, ActiveWorkbook.FullName, ":") > 0 Then
'File has been saved. Show the Save dialog box and
'put path in caption.
ActiveWorkbook.Save
Application.ActiveWindow.Caption = ActiveWorkbook.FullName
Else
'file has not been saved. Show SaveAs dialog box and
'put path in caption.
Application.Dialogs(xlDialogSaveAs).Show
Application.ActiveWindow.Caption = ActiveWorkbook.FullName
End If
End Sub
Sub Opener()
Application.Dialogs(xlDialogOpen).Show
Application.ActiveWindow.Caption = ActiveWorkbook.FullName
End Sub
- Press ALT+F11 to return to Microsoft Excel.
Using the Macro to Open a File
To open an existing file, follow these steps:
- On the Tools menu, point to Macro, and then click Macros.
- Click Opener and then click Run.
- In the Open dialog box, select the Excel file you want to open and then click Open.
The full path and file name will appear in the title bar
of the opened workbook.
Using the Macro to Save a File
To save a new file, follow these steps:
- On the Tools menu, point to Macro, and then click Macros.
- Click Saver and then click Run.
- In the Save dialog box, select the location where you want to save the file and click Save.
The full path and file name will appear in the title bar of the saved workbook.
Running the Macro Automatically When You Start Excel
To run the macro automatically when you start Excel, save the
workbook with the new code in the Xlstart folder.
Assigning the Macros to Toolbar Buttons
To customize Excel and assign the macros to the built-in toolbar buttons, follow these steps:
- Start Excel.
- On the Tools menu, click Customize.
- In the Customize dialog box, click the Commands tab.
- On the Standard toolbar above, click the Save button.
- In the Customize dialog box, click Modify Selection.
A menu appears with different options for your toolbar buttons. - Click Assign Macro. In the Macro Name list, click Saver. Click OK.
- On the Standard toolbar, click the Open button.
- In the Customize dialog box, click Modify Selection.
- Click Assign Macro. In the Macro Name list, click Opener. Click OK.
Assigning the Macros to Built-in Menus
To assign these macros to the built-in commands on the
File menu, follow these steps:
- Start Excel.
- On the Tools menu, click Customize.
- In the Customize dialog box, click the Toolbars tab.
- Click to select the Worksheet Menu Bar if it is not already checked.
- On the File menu, click Open.
- In the Customize dialog box, click the Commands tab.
- In the Customize dialog box, click Modify Selection.
- Click Assign Macro. In the Macro Name list, click Opener. Click OK.
- On the File menu, click Save.
- In the Customize dialog box, click the Commands tab.
- In the Customize dialog box, click Modify Selection.
- Click Assign Macro. In the Macro Name list, click Saver. Click OK.
- On the File menu, click Save.
- In the Customize dialog box, click Modify Selection.
- Click Assign Macro. In the Macro Name list, click SaveAser. Click OK.
- In the Customize dialog box, click Close.
Having the Macros Available the Next Time You Start Excel
To have the macros available when you start Excel,
hide the workbook. To do this, click
Hide on the
Windows menu. When you click
Exit on the
File menu, click
Yes if you are prompted to save changes to the hidden workbook.