How to check and remove incorrect project references in the Visual Basic Editor in Word 2002 (308340)
The information in this article applies to:
This article was previously published under Q308340 SUMMARY 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.
back to the top
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
projects.
- Click OK.
back to the top
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:
- 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.
- Change the File name box to Refme.
- Change the Save as type box to
Document Template (*.dot).
- Save the template in the "C:\TestFiles" folder (you
must create this folder if it does not exist).
- On the Tools menu, point to Macro, and then click Visual Basic Editor (or press ALT+F11) 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
project.
- In the Code window, type the following code for Module1:
Public Sub CallMe()
Msgbox "Hello World!"
End Sub
- Click the sub procedure, and then, on the Run menu, click Run Sub/UserForm. A "Hello World!" message box appears. Close this message
box.
- 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).
- 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()
Call CallMe
End Sub
- Test the procedure to verify that it works, and then return
to Word.
- Save and close the document file, and then quit
Word.
back to the top
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
"Myproj.doc".
- 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.
- 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
"missing".
- 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:
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
Next
End Sub
- 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.
back to the top
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
your project:
vbProj.References.Remove chkRef
- 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
"Refme.dot".
- Return to the Myproj project in the Visual Basic Editor in
Word.
- Write a new sub procedure named "AddReference" as follows:
Sub AddReference()
Dim vbProj as VBProject
Set vbProj = ActiveDocument.VBProject
vbProj.References.AddFromFile "C:\TestFiles\Refme.dot"
End Sub
- 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.
back to the top
Troubleshooting- 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
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.
back to the top
Modification Type: | Minor | Last Reviewed: | 9/8/2004 |
---|
Keywords: | kbProgramming KbVBA kbHOWTOmaster KB308340 kbAudDeveloper |
---|
|