Freigeben über


How to: Programmatically 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 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Closing the Active Document

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.

    Note

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

    Me.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
    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.

    Note

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

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

Closing a Document That You Specify By Name

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.

    Note

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

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

See Also

Tasks

How to: Programmatically Open Existing Documents

How to: Programmatically Save Documents

Concepts

Host Items and Host Controls Overview

Programmatic Limitations of Host Items and Host Controls

Optional Parameters in Office Solutions