Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: How To Add the Workbook Path to the Title Bar


View products that this article applies to.

This article was previously published under Q272717

↑ Back to the top


Summary

This article provides a sample Microsoft Visual Basic for Applications macro that adds the workbook path and file name to the title bar of a workbook when opening or saving a file.

↑ Back to the top


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.

Sample Code

To add the saved document file name and path to the title bar of the active document, follow these steps:
  1. Save and close any open workbooks, and then create a new workbook.
  2. Start the Visual Basic Editor (press ALT+F11).
  3. On the Insert menu, click Module.
  4. 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
    					
  5. Press ALT+F11 to return to Microsoft Excel.

Using the Macro to Open a File

To open an existing file, follow these steps:
  1. On the Tools menu, point to Macro, and then click Macros.
  2. Click Opener and then click Run.
  3. 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:
  1. On the Tools menu, point to Macro, and then click Macros.
  2. Click Saver and then click Run.
  3. 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:
  1. Start Excel.
  2. On the Tools menu, click Customize.
  3. In the Customize dialog box, click the Commands tab.
  4. On the Standard toolbar above, click the Save button.
  5. In the Customize dialog box, click Modify Selection.

    A menu appears with different options for your toolbar buttons.
  6. Click Assign Macro. In the Macro Name list, click Saver. Click OK.
  7. On the Standard toolbar, click the Open button.
  8. In the Customize dialog box, click Modify Selection.
  9. 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:
  1. Start Excel.
  2. On the Tools menu, click Customize.
  3. In the Customize dialog box, click the Toolbars tab.
  4. Click to select the Worksheet Menu Bar if it is not already checked.
  5. On the File menu, click Open.
  6. In the Customize dialog box, click the Commands tab.
  7. In the Customize dialog box, click Modify Selection.
  8. Click Assign Macro. In the Macro Name list, click Opener. Click OK.
  9. On the File menu, click Save.
  10. In the Customize dialog box, click the Commands tab.
  11. In the Customize dialog box, click Modify Selection.
  12. Click Assign Macro. In the Macro Name list, click Saver. Click OK.
  13. On the File menu, click Save.
  14. In the Customize dialog box, click Modify Selection.
  15. Click Assign Macro. In the Macro Name list, click SaveAser. Click OK.
  16. 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.

↑ Back to the top


References

For additional information about getting help with Visual Basic for Applications, click the article number below to view the article in the Microsoft Knowledge Base:
163435� Programming Resources for Visual Basic for Applications
For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
212536� OFF2000: How to Run Sample Code from Knowledge Base Articles

↑ Back to the top


Keywords: KB272717, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 272717
Revision : 7
Created on : 7/4/2007
Published on : 7/4/2007
Exists online : False
Views : 237