In the Microsoft Word Visual Basic Editor, you can create references to object libraries or code in an external document or template. This allows you to call external procedures as if they were written in your own project. Note that problems can occur if the referenced files are moved or become corrupted. Macro storage errors can occur, and the References dialog box can become inaccessible.

This article describes how to create references to other Microsoft Visual Basic for Applications (VBA) projects, how to verify that the references are correct, and how to programmatically remove incorrect references.

Prior knowledge that is required:

  • Fundamental VB/VBA programming skills

  • Basic Microsoft Office skills

  • Windows Explorer skills

In this article, you use the Visual Basic Editor to demonstrate how to:

  • Create a reference to your own custom VBA project.

  • Write code to verify that the reference exists.

  • Write code to remove an incorrect reference.

  • Write code to add the reference back.

Set Up the Project Security Permissions

  1. In Microsoft Word, on the Tools menu, point to Macro, and then click Security to display the Security dialog box.

  2. On the Security Level tab, set the security level to Medium, so that you have the choice of enabling your macros.

  3. On the Trusted Sources tab, verify that the Trust access to Visual Basic Project check box is selected. This allows you to use VBA code in your projects.

  4. Click OK.

In Word 2007, follow these steps:

  1. On the Developer tab, click Macro Security in the Code group.

  2. In the left pane, click Macro Settings.

  3. In the right pane, click Enable all macros, and then click to select the Trust access to the VBA project object model check box.

  4. Click OK.

Create the Project Reference

You will create a project that contains a procedure and then create a reference to that project from a new VBA project.

To create the library file:

  1. Create a new document in Microsoft Word, and then save it as a template by following these steps:

    1. On the File menu, click Save As to display the Save As dialog box.

      Note In Word 2007, click the Microsoft Office Button, and then click Save As.

    2. Change the File name box to Refme.

    3. Change the Save as type box to
      Document Template (*.dot).

      Note In Word 2007, change the Save as type box to
      Word 97-2003 Template (*.dot)

    4. Save the template in the "C:\TestFiles" folder (you must create this folder if it does not exist).

  2. Press ALT+F11 to start the Visual Basic Editor.

  3. In the Project Explorer, select TemplateProject (Refme). On the Insert menu, click Module. This adds a new module named "Module1" to the project.

  4. In the Code window, type the following code for Module1:

    Public Sub CallMe()

    Msgbox "Hello World!"

    End Sub
  5. Click the sub procedure, and then, on the Run menu, click Run Sub/UserForm. A "Hello World!" message box appears. Close this message box.

  6. On the View menu, click Microsoft Word (ALT+F11) to return to Word.

  7. In Word, save and close the template file.

To create the reference:

  1. In Word, create a new document, and then save it as "Myproj.doc" in the "C:\TestFiles" folder.

  2. Start the Visual Basic Editor (press ALT+F11). In the Project Explorer, select Project (Myproj).

  3. Create a reference to the Refme template as follows:

    1. On the Tools menu, click References.

    2. In the References dialog box, click Browse.

    3. In the Add Reference dialog box, set Files of type to Word Documents (*.doc; *.dot).

      Note In Word 2007, set Files of type to Word Documents (*.docm;*.dotm*.doc; *.dot).

    4. In the "C:\TestFiles" folder, select the Refme template.

    5. Click Open to return to the References dialog box.

    6. Click OK to return to the Visual Basic Editor.

  4. In the Project Explorer, expand Project (Myproj). Expand References and verify that the reference list includes Reference to

  5. On the Insert menu, click Module to insert a new module into the project. Write the following code in the module to call the CallMe procedure:

    Public Sub Test()

    Call CallMe

    End Sub
  6. Test the procedure to verify that it works, and then return to Word.

  7. Save and close the document file, and then quit Word.

Programmatically Check the Project Reference

You now will deliberately break your reference and then verify the accuracy of your reference.

To break the reference:

  1. In Windows Explorer (right-click the Windows Start menu and then click Explore on the shortcut menu), change the name of the template to "Refme.old". To do this, right-click and then click Rename on the shortcut menu. Type refme.old and then press ENTER.

  2. Return to Word, and then reopen the document "Myproj.doc".

  3. Because your security level is set to Medium, you will be asked whether you want to enable or disable macros in this document. Click Enable Macros.

    NoteThis behavior does not occur in Word 2007.

  4. Start the Visual Basic Editor (press ALT+F11), and then run the Test procedure. Click OK to the following compile error message:

    Can't find project or library

  5. You should be in Break mode (there is a yellow band across the Sub line). On the Run menu, click Reset to return to Design mode.

  6. On the Tools menu, click References to display the References dialog box. You see that is marked as "missing".

  7. Click Cancel to close the References dialog box.

To programmatically check for missing references:

  1. In the Project Explorer, select Project (Myproj).

  2. On the Tools menu, click References and then add a reference to the Microsoft Visual Basic for Applications Extensibility 5.3 library. This library contains objects that refer to the VBA projects.

  3. Before you click OK, you must verify that the new reference is added above the "missing" reference. Click the arrow several times to change the priority, move the new reference above the "missing" reference, and then click OK.

  4. In the module, create a new sub procedure named "CheckReference", and then write the following code:

    Sub CheckReference()

    Dim vbProj As VBProject ' This refers to your VBA project.
    Dim chkRef As Reference ' A reference.

    ' Refer to the activedocument's VBA project.
    Set vbProj = ActiveDocument.VBProject

    ' Check through the selected references in the References dialog box.
    For Each chkRef In vbProj.References

    ' If the reference is broken, send the name to the Immediate Window.
    If chkRef.IsBroken Then
    Debug.Print chkRef.Name
    End If


    End Sub
  5. On the View menu, click Immediate Window (CTRL+G) to open the Immediate Window.

  6. Run the CheckReference sub procedure. The name and location of the file is displayed in the Immediate Window.

Remove and Reinstate the Project Reference

This section describes how to programmatically remove and reinstate a reference.

To remove a missing reference:

  1. In the CheckReference sub procedure, change the Debug.Print statement as follows. This removes any missing references from your project:

       vbProj.References.Remove chkRef
  2. Run the CheckReference procedure.

  3. Verify that the reference was removed in the References dialog box (on the Tools menu, click References).

To reinstate (add) a reference:

  1. In Windows Explorer, rename Refme.old back to "".

  2. Return to the Myproj project in the Visual Basic Editor in Word.

  3. Write a new sub procedure named "AddReference" as follows:

    Sub AddReference()

    Dim vbProj as VBProject

    Set vbProj = ActiveDocument.VBProject
    vbProj.References.AddFromFile "C:\TestFiles\"

    End Sub
  4. Run the AddReference procedure.

  5. Verify that the reference to was reinstated. You can view references in Project Explorer or when you click References on the Tools menu.


  • If you make references to an application's native file, you should keep the native file extension. Otherwise, you may experience problems when you use the reference. To see these problems, try to make a reference to the Refme.old file instead of the file in the example earlier in this article.

  • If you are working with a registered component, you can add a GUID reference to your project. Use the AddFromGUID method to do this.

Need more help?

Expand your skills
Explore Training
Get new features first
Join Microsoft Insiders

Was this information helpful?

What affected your experience?

Thank you for your feedback!