Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Close Documents

  Switch on low bandwidth view
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: Close Documents

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 close the active document or you can specify a document to close.

There are two procedures for closing the active document: one for document-level customizations and one for application-level add-ins.

To close the active document in a document-level customization

  • Call the Document..::.Close method to close the document associated with the customization. To use the following code example, run it in the ThisDocument class in a document-level project for Word.

    NoteNote:

    This example passes the wdDoNotSaveChanges value to the SaveChanges parameter to close without saving changes or prompting the user.

    Visual Basic
    Me.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
    
    C#
    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; 
    this.Close(ref doNotSaveChanges, ref missing, ref missing);
    
    

To close the active document in an application-level add-in

  • Call the _Document..::.Close method to close the active document. To use the following code example, run it in the ThisAddIn class in an application-level project for Word.

    NoteNote:

    This example passes the wdDoNotSaveChanges value to the SaveChanges parameter to close without saving changes or prompting the user.

    Visual Basic
    Me.Application.ActiveDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
    
    C#
    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    Word._Document document = this.Application.ActiveDocument;
    document.Close(ref doNotSaveChanges, ref missing, ref missing);
    
    

The way that you close a document that you specify by name is the same for application-level add-ins and document-level customizations.

To close a document that you specify by name

  • Specify the document name as an argument to the Documents collection. The following code example assumes that a document named NewDocument is open in Word.

    NoteNote:

    This example passes the wdDoNotSaveChanges value to the SaveChanges parameter to close without saving changes or prompting the user.

    Visual Basic
    Application.Documents("NewDocument.doc").Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
    
    C#
    object fileName = "NewDocument.doc";
    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    Word.DocumentClass doc = Application.Documents.get_Item(ref fileName) as Word.DocumentClass;
    
    doc.Close(ref doNotSaveChanges, ref missing, ref missing);
    
    
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker