XL97: How to Programmatically Create a Reference

Last reviewed: February 27, 1998
Article ID: Q160647
The information in this article applies to:
  • Microsoft Excel 97 for Windows

SUMMARY

This article provides sample Microsoft Visual Basic for Applications Sub procedures (or macros) that programmatically create and remove a reference to another project.

NOTE: This functionality was not available in Microsoft Excel for Windows 95, version 7.0 or earlier.

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 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/supportnet/refguide/

Creating a Reference to Another Project

  1. Close all open workbooks, and then open two new workbooks.

  2. Save the workbooks as Book1.xls and Book2.xls in the
     C:\My Documents folder.

  3. Start the Visual Basic Editor (press ALT+F11).

  4. If the Project window is not visible, click Project Explorer
     on the View menu.

  5. In the Project window, click "VBAProject (Book1.xls)."

  6. On the Insert menu, click Module.

     This step adds a module in the Book1.xls project.

  7. In the module, type the following code:

       Sub Create_Reference()

           Application.VBE.ActiveVBProject.References.AddFromFile _
               "C:\My Documents\Book2.xls"

           Use_Book2_Reference

       End Sub

       Sub Use_Book2_Reference()

           'Make calls to Book2.xls objects here.

       End Sub

     NOTE: You cannot make reference calls to objects that are contained in
     the object library added programmatically. You must run another
     procedure that makes the calls.

  8. In the Project window, click "VBAProject (Book2.xls)."

  9. If the Properties window is not visible, click Properties Window on
     the View menu.

 10. Next to the "(Name)" property, type "Project2" (without the quotation
     marks).

     NOTE: If you want to create a reference to another project, that
     project cannot use the same value for the "(Name)" property.

     For additional information, please see the following articles in the
     Microsoft Knowledge Base:

       ARTICLE-ID: Q156882
       TITLE     : XL97 Err Msg: "Name Conflicts with Existing Module"

 11. Run the Create_Reference macro you created in step 7.

     This step creates a reference in Book1.xls to the project in
     Book2.xls.

 12. To verify the reference, click References on the Tools menu.

     In the "References - VBAProject" dialog box, the "Project2" check box
     should be selected.

 13. Click OK.

Removing a Reference to Another Project

The following steps assume you followed all the steps in the "Creating a Reference to Another Project" section.

  1. In the module sheet in which you typed the Create_Reference macro, type the following code:

          Sub Remove_Reference()
    
              Dim x As Object
              Set x = Application.VBE.ActiveVBProject _
                 .References.Item("Project2")
    
              Application.VBE.ActiveVBProject.References.Remove x
    
          End Sub
    
    

  2. Run the Remove_Reference macro.

    This step removes the reference to the project in Book2.xls.

  3. To verify whether Microsoft Excel removed the reference, click References on the Tools menu.

    In the "References - VBAProject" dialog box, the "Project2" check box should be cleared.

  4. Click OK.

REFERENCES

For more information about the References object, create a reference to the "Microsoft Visual Basic for Applications Extensibility" library, and then search the Object Browser. To do this, use the following steps:

  1. On the Tools menu, click References.

  2. In the References dialog box, click the "Microsoft Visual Basic for Applications Extensibility" check box, and then click OK.

  3. In the Object Browser, search the VBIDE library for the References ' class.

For additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q163435
   TITLE     : VBA: Programming Resources for Visual Basic for
               Applications


Additional query words: 97 XL97 tools references
Keywords : kbprg xlvbahowto xlvbainfo kbhowto
Version : WINDOWS:97
Platform : WINDOWS
Solution Type : kbcode


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 27, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.