Skip to content

Latest commit

 

History

History
50 lines (39 loc) · 2.39 KB

how-to-programmatically-list-all-worksheets-in-a-workbook.md

File metadata and controls

50 lines (39 loc) · 2.39 KB
title description titleSuffix ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Programmatically list all worksheets in a workbook
Learn how you can programmatically list all worksheets in a Microsoft Excel workbook by using Visual Studio.
02/02/2017
how-to
VB
CSharp
workbooks, listing worksheets
worksheets, listing
John-Hart
johnhart
mijacobs
office-development

Programmatically list all worksheets in a workbook

The xref:Microsoft.Office.Interop.Excel.Workbook class provides a xref:Microsoft.Office.Interop.Excel.Worksheets object. This object contains a collection of all the xref:Microsoft.Office.Interop.Excel.Worksheet objects in the workbook.

[!INCLUDEappliesto_xlalldocapp]

To list all existing worksheets in a workbook in a document-level customization

  1. Iterate through the xref:Microsoft.Office.Interop.Excel.Worksheets collection and send the name of each sheet to a cell offset from a xref:Microsoft.Office.Tools.Excel.NamedRange control.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_VstcoreExcelAutomationCS/Sheet1.cs" id="Snippet21":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_VstcoreExcelAutomation/Sheet1.vb" id="Snippet21":::

To list all existing worksheets in a workbook in a VSTO Add-in

  1. Iterate through the xref:Microsoft.Office.Interop.Excel.Worksheets collection and send the name of each sheet to a cell offset from a xref:Microsoft.Office.Interop.Excel.Range object.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/trin_vstcoreexcelautomationaddin/ThisAddIn.cs" id="Snippet13":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/trin_vstcoreexcelautomationaddin/ThisAddIn.vb" id="Snippet13":::

Related content