Bagikan melalui


How to: Activate 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.

The Activate method of the Workbooks collection activates a Microsoft Office Excel workbook and selects the first sheet in the workbook.

To activate a workbook

  • Use the Activate method of the Workbooks collection.

    Me.Application.Workbooks(1).Activate()
    
    ((Microsoft.Office.Interop.Excel._Workbook)
        this.Application.Workbooks[1]).Activate();
    

You can index into the Workbooks collection using either integers (indicating the position within the collection) or workbook names. If you want to refer to a workbook by name, you must use the name as it appears in the title bar, which does not include the file name extension until you save the file.

To reference individual workbooks

  • Use the position number or the name of the workbook.

    Dim wb As Excel.Workbook = Me.Application.Workbooks(1)
    
    ' Before Book1 is saved:
    wb = Me.Application.Workbooks("Book1")
    
    ' After Book1 is saved:
    wb = Me.Application.Workbooks("Book1.xls")
    
    Excel.Workbook wb = this.Application.Workbooks[1];
    
    // Before Book1 is saved:
    wb = this.Application.Workbooks["Book1"];
    
    // After Book1 is saved:
    wb = this.Application.Workbooks["Book1.xls"];
    

To activate the workbook associated with a document-level project

  • In document-level projects only, activate the workbook by using ThisWorkbook.

    Globals.ThisWorkbook.Activate()
    
    Globals.ThisWorkbook.Activate();
    

See Also

Tasks

How to: Open Workbooks

How to: Save Workbooks

How to: Close Workbooks

How to: Select Worksheets

How to: Protect Workbooks

Concepts

Working with Workbooks

Host Items and Host Controls Overview

Programmatic Limitations of Host Items and Host Controls

Host Items and Host Controls Overview