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.
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.
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
- Create a reference to your own custom VBA
- 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
- In Microsoft Word, on the Tools menu, point to Macro, and then click Security to display the Security dialog box.
- On the Security Level tab, set the security level to Medium, so that you have the choice of enabling your macros.
- 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
- Click OK.
Word 2007, follow these steps:
- On the Developer tab, click Macro
Security in the Code group.
- In the left pane, click Macro Settings.
- In the right pane, click Enable all
macros, and then click to select the Trust access
to the VBA project object model check box.
- 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.
the library file:
- Create a new document in Microsoft Word, and then save it
as a template by following these steps:
- On the File menu, click Save As to display the Save As dialog box.
Word 2007, click the Microsoft Office Button, and then click Save
- Change the File name box to Refme.
- 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)
- Save the template in the "C:\TestFiles" folder (you
must create this folder if it does not exist).
to start the Visual Basic Editor.
- In the Project Explorer, select TemplateProject (Refme).
On the Insert menu, click Module. This adds a new module named "Module1" to the
- In the Code window, type the following code for Module1:
Public Sub CallMe()
Msgbox "Hello World!"
- Click the sub procedure, and then, on the Run menu, click Run Sub/UserForm. A "Hello World!" message box appears. Close this message
- On the View menu, click Microsoft Word (ALT+F11) to return to Word.
- In Word, save and close the template file.
To create the reference:
- In Word, create a new document, and then save it as
"Myproj.doc" in the "C:\TestFiles" folder.
- Start the Visual Basic Editor (press ALT+F11). In the
Project Explorer, select Project (Myproj).
- Create a reference to the Refme template as follows:
- On the Tools menu, click References.
- In the References dialog box, click Browse.
- 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).
- In the "C:\TestFiles" folder, select the Refme template.
- Click Open to return to the References dialog box.
- Click OK to return to the Visual Basic Editor.
- In the Project Explorer, expand Project (Myproj). Expand References and verify that the reference list includes Reference to Refme.dot.
- 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()
- Test the procedure to verify that it works, and then return
- Save and close the document file, and then quit
Programmatically Check the Project Reference
You now will deliberately break your reference and then verify
the accuracy of your reference.
To break the reference:
- In Windows Explorer (right-click the Windows Start menu and then click Explore on the shortcut menu), change the name of the Refme.dot template
to "Refme.old". To do this, right-click Refme.dot and then click Rename on the shortcut menu. Type refme.old and
then press ENTER.
- Return to Word, and then reopen the document
- 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.
This behavior does not occur in Word 2007.
- 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
- 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.
- On the Tools menu, click References to display the References dialog box. You see that Refme.dot is marked as
- Click Cancel to close the References dialog box.
To programmatically check for missing references:
- In the Project Explorer, select Project (Myproj).
- 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.
- 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.
- In the module, create a new sub procedure named
"CheckReference", and then write the following code:
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
- On the View menu, click Immediate Window (CTRL+G) to open the Immediate Window.
- 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:
- In the CheckReference sub procedure, change the Debug.Print statement as follows. This removes any missing references from
- Run the CheckReference procedure.
- Verify that the reference was removed in the References dialog box (on the Tools menu, click References).
To reinstate (add) a reference:
- In Windows Explorer, rename Refme.old back to
- Return to the Myproj project in the Visual Basic Editor in
- Write a new sub procedure named "AddReference" as follows:
Dim vbProj as VBProject
Set vbProj = ActiveDocument.VBProject
- Run the AddReference procedure.
- Verify that the reference to Refme.dot 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 Refme.dot file in the example earlier in this
- If you are working with a registered component, you can add
a GUID reference to your project. Use the AddFromGUID method to do this.
Article ID: 308340 - Last Review: November 10, 2006 - Revision: 2.0
- Microsoft Office Word 2007
- Microsoft Office Word 2003
- Microsoft Word 2002 Standard Edition
|kbprogramming kbvba kbhowtomaster KB308340|