Megosztás a következőn keresztül:


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.

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 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.

    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 _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.

    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)
    
    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
    Word._Document document = this.Application.ActiveDocument;
    document.Close(ref doNotSaveChanges, ref missing, ref missing);
    

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. 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.doc").Close(Word.WdSaveOptions.wdDoNotSaveChanges)
    
    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);
    

See Also

Tasks

How to: Open Existing Documents

How to: Save Documents

Concepts

Host Items and Host Controls Overview

Programmatic Limitations of Host Items and Host Controls

The Variable missing and Optional Parameters in Office Solutions