How to: Close Workbooks

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

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

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

Closing the Active Workbook

There are two procedures for closing the active workbook: one for document-level customizations and one for application-level add-ins.

To close the active workbook in a document-level customization

  • Call the Close method to close the workbook associated with the customization. To use the following code example, run it in the Sheet1 class in a document-level project for Excel.

    Globals.ThisWorkbook.Close(SaveChanges:=False)
    
    Globals.ThisWorkbook.Close(false, missing, missing);
    

To close the active workbook in an application-level add-in

  • Call the Close(Object, Object, Object) method to close the active workbook. To use the following code example, run it in the ThisAddIn class in an application-level project for Excel.

    Me.Application.ActiveWorkbook.Close(SaveChanges:=False)
    
    this.Application.ActiveWorkbook.Close(false, missing, missing);
    

Closing a Workbook That You Specify By Name

The way that you close a workbook that you specify by name is the same for application-level add-ins and document-level customizations.

To close a workbook that you specify by name

  • Specify the workbook name as an argument to the Workbooks collection. The following code example assumes that a workbook named NewWorkbook is open in Excel.

    Me.Application.Workbooks("NewWorkbook.xls").Close(SaveChanges:=False)
    
    object fileName = "NewWorkbook.xls";
    Excel.Workbook workbook = this.Application.Workbooks.get_Item(fileName);
    workbook.Close(false, missing, missing);
    

See Also

Tasks

How to: Save Workbooks

How to: Open Workbooks

How to: Activate Workbooks

Concepts

Working with Workbooks

Programmatic Limitations of Host Items and Host Controls

The Variable missing and Optional Parameters in Office Solutions

Host Items and Host Controls Overview

Change History

Date

History

Reason

July 2008

Added a code example that can be used in an application-level add-in.

Customer feedback.