Export (0) Print
Expand All
Expand Minimize

How to: Close Documents

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

You can use the Close method to close Microsoft Office Word 2003 documents and specify whether you want to save the document when you close it.

The document can be one of two types:

Using Host Items

The following code assumes you have written the code in the ThisDocument class.

To close the active document without saving changes

  • Call the Close method to specify the document associated with the project.

    NoteNote

    This example includes the wdDoNotSaveChanges argument of the Close method to close without saving changes or prompting the user.

    object doNotSaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; 
    this.Close(ref doNotSaveChanges, ref missing, ref missing);
    
    

Using Word Objects

The following code assumes you have programmatically added a Microsoft.Office.Interop.Word.Document to your project and named it NewDocument.

To close a document and save changes without prompting

  • Specify the document name as an argument to the Documents collection.

    NoteNote

    This example includes the wdSaveChanges argument of the Close method to save without prompting.

    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

Community Additions

ADD
Show:
© 2014 Microsoft