|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Reference Documents in the Documents Collection
The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.
Microsoft Office version
For more information, see Features Available by Application and Project Type.
Referring to a document by its name is usually the better choice if you want to work with a specific document. You rarely refer to a document by using its index value in the Documents collection because this value can change for a given document as other documents are opened and closed.
To reference a document by its name
Set an object variable to point to the named document, SampleDoc.doc. To use the following code example, run the code from the ThisDocument or ThisAddIn class in your project.
To reference the active document
Retrieve the name of the active document using ActiveDocument. To use the following code example, run the code from the ThisDocument or ThisAddIn class in your project.
If you are only working with the document that is associated with a document-level customization, you can use the Globals.ThisDocument reference from anywhere in the project when you need to refer to the document that has the focus.