Microsoft KB Archive/308340

= How to check and remove incorrect project references in the Visual Basic Editor in Word =

Article ID: 308340

Article Last Modified on 11/10/2006

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q308340





IN THIS TASK
SUMMARY
 * Set Up the Project Security Permissions
 * Create the Project Reference
 * Programmatically Check the Project Reference
 * Remove and Reinstate the Project Reference
 * Troubleshooting



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

 * 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.

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.

Note In Word 2007, click the Microsoft Office Button, and then click Save As. 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 &quot;C:\TestFiles&quot; folder (you must create this folder if it does not exist). </li> Press ALT+F11 to start the Visual Basic Editor.</li> In the Project Explorer, select TemplateProject (Refme). On the Insert menu, click Module. This adds a new module named &quot;Module1&quot; to the project.</li>  In the Code window, type the following code for Module1: Public Sub CallMe

Msgbox &quot;Hello World!&quot;

End Sub </li> Click the sub procedure, and then, on the Run menu, click Run Sub/UserForm. A &quot;Hello World!&quot; message box appears. Close this message box.</li> On the View menu, click Microsoft Word (ALT+F11) to return to Word.</li> In Word, save and close the template file.</li></ol>

To create the reference:  In Word, create a new document, and then save it as &quot;Myproj.doc&quot; in the &quot;C:\TestFiles&quot; folder.</li> Start the Visual Basic Editor (press ALT+F11). In the Project Explorer, select Project (Myproj).</li> Create a reference to the Refme template as follows:  On the Tools menu, click References.</li> In the References dialog box, click Browse.</li> 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).</li> In the &quot;C:\TestFiles&quot; folder, select the Refme template.</li> Click Open to return to the References dialog box.</li> <li>Click OK to return to the Visual Basic Editor.</li></ol> </li> <li>In the Project Explorer, expand Project (Myproj). Expand References and verify that the reference list includes Reference to Refme.dot.</li> <li> 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 </li> <li>Test the procedure to verify that it works, and then return to Word.</li> <li>Save and close the document file, and then quit Word.</li></ol>

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: <ol> <li>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 &quot;Refme.old&quot;. To do this, right-click Refme.dot and then click Rename on the shortcut menu. Type refme.old and then press ENTER.</li> <li>Return to Word, and then reopen the document &quot;Myproj.doc&quot;.</li> <li>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.

Note This behavior does not occur in Word 2007.</li> <li>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

</li> <li>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.</li> <li>On the Tools menu, click References to display the References dialog box. You see that Refme.dot is marked as &quot;missing&quot;.</li> <li>Click Cancel to close the References dialog box.</li></ol>

To programmatically check for missing references: <ol> <li>In the Project Explorer, select Project (Myproj).</li> <li>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.</li> <li>Before you click OK, you must verify that the new reference is added above the &quot;missing&quot; reference. Click the arrow several times to change the priority, move the new reference above the &quot;missing&quot; reference, and then click OK.</li> <li> In the module, create a new sub procedure named &quot;CheckReference&quot;, 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 </li> <li>On the View menu, click Immediate Window (CTRL+G) to open the Immediate Window.</li> <li>Run the CheckReference sub procedure. The name and location of the file is displayed in the Immediate Window.</li></ol>

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: <ol> <li> In the CheckReference sub procedure, change the Debug.Print statement as follows. This removes any missing references from your project: vbProj.References.Remove chkRef </li> <li>Run the CheckReference procedure.</li> <li>Verify that the reference was removed in the References dialog box (on the Tools menu, click References).</li></ol>

To reinstate (add) a reference: <ol> <li>In Windows Explorer, rename Refme.old back to &quot;Refme.dot&quot;.</li> <li>Return to the Myproj project in the Visual Basic Editor in Word.</li> <li> Write a new sub procedure named &quot;AddReference&quot; as follows: Sub AddReference

Dim vbProj as VBProject

Set vbProj = ActiveDocument.VBProject vbProj.References.AddFromFile &quot;C:\TestFiles\Refme.dot&quot;

End Sub </li> <li>Run the AddReference procedure.</li> <li>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.</li></ol>

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

Keywords: kbprogramming kbvba kbhowtomaster KB308340

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.