Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Reference Documents in the ...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
How to: Reference Documents in the Documents Collection

Updated: November 2007

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can refer to a Document object as a member of the Documents collection by using the name of the document or its index value in the collection.

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.

    Visual Basic
    Dim doc As Word.Document = CType(Application.Documents("SampleDoc.doc"), Word.Document)
    
    
    C#
    object fileName = "SampleDoc.doc"; 
    Word.Document doc = Application.Documents.get_Item(ref fileName); 
    
    

If you want to refer to the active document (the document that has focus), you can use the ActiveDocument()()() property of the Application object.

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.

    Visual Basic
    Dim documentName As String = Application.ActiveDocument.Name
    
    
    C#
    string documentName = Application.ActiveDocument.Name;
    
    

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.

To reference the document associated with a document-level customization

  • Retrieve the name of the document using Globals.ThisDocument.

    Visual Basic
    Dim documentName2 As String = Globals.ThisDocument.Name
    
    
    C#
    string documentName2 = Globals.ThisDocument.Name;
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker