Export (0) Print
Expand All

How to: Close Documents

You can close the active document or you can specify a document to close.

Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2007 and Word 2010. For more information, see Features Available by Office Application and Project Type.

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 Close method of the ThisDocument class in your project to close the document associated with the customization. To use the following code example, run it from the ThisDocument class.

    NoteNote

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

    
    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 Close method of the ActiveDocument property to close the active document. To use the following code example, run it from the ThisAddIn class in your project.

    NoteNote

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

    
    Word._Document document = this.Application.ActiveDocument;
    document.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
    
    
    

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, and then call the Close method. 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.

    
    Word._Document doc = Application.Documents["NewDocument.doc"] as Word._Document;
    doc.Close(Word.WdSaveOptions.wdDoNotSaveChanges);
    
    
    

Community Additions

ADD
Show:
© 2014 Microsoft