Microsoft KB Archive/184579

= XL98: How to Programmatically Create a Reference =

Article ID: 184579

Article Last Modified on 9/12/2006

-

APPLIES TO


 * Microsoft Excel 98 for Macintosh

-



This article was previously published under Q184579





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



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.

Creating a Reference to Another Project
 Close all open workbooks, and then create two new workbooks. Save the workbooks as Book1 and Book2 in your :Microsoft Office 98 folder, where is the name of your hard disk. Start the Visual Basic Editor (press OPTION+F11). If the Project window is not visible, click Project Explorer on the View menu. In the Project window, click "VBAProject (Book1)." On the Insert menu, click Module.

This step adds a module in the Book1 project.</li>  In the module, type the following code, where is the name of your hard disk: Sub Create_Reference

Application.VBE.ActiveVBProject.References.AddFromFile _ " :Microsoft Office 98:Book2"

Use_Book2_Reference

End Sub

Sub Use_Book2_Reference

'Make calls to Book2 objects here.

End Sub NOTE: You cannot make reference calls to objects that have been added to the object library programmatically in the same procedure. You must run another procedure to make the calls. </li> In the Project window, click "VBAProject (Book2)."</li> If the Properties window is not visible, click Properties Window on the View menu.</li> Next to the "(Name)" property, type Project2.

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:

183132 XL98 Err Msg: "Name Conflicts with Existing Module"

</li> Run the Create_Reference macro you created in step 7.

This step creates a reference in Book1 to the project in Book2.</li> To verify the reference, click References on the Tools menu.

In the "References - VBAProject" dialog box, the "Project2" check box should be selected.</li> Click OK.</li></ol>

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

<ol>  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 </li> Run the Remove_Reference macro.

This step removes the reference to the project in Book2.</li> 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.</li> Click OK.</li></ol>

<div class="references_section">