Export (0) Print
Expand All

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.

    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 _DocumentClose(Object, Object, Object) 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.

    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.

    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);
    

Community Additions

ADD
Show:
© 2014 Microsoft