to get all sheet Ids on workbook

1,380 views
Skip to first unread message

Sam.Park

unread,
Jun 6, 2011, 10:44:45 PM6/6/11
to Excel-DNA
Hi

I can get sheet id on specific sheet using below code

ExcelReference caller = XlCall.Excel(XlCall.xlfCaller) as
ExcelReference;
int id = caller.SheetId.ToInt32();

I wonder how i can get all sheet ids on workbook and i also wonder
whether sheet Ids are not

altered when sheet name is changed or new worksheet is added ?

Thanks
Park

Govert van Drimmelen

unread,
Jun 7, 2011, 2:40:53 AM6/7/11
to Excel-DNA
Hi Park,

You can get all the sheet names in a book with the C API using the
GET.WORKBOOK function (xlfGetWorkbook). There was more on using the
Get.XXX information functions in this discussion:
http://groups.google.com/group/exceldna/browse_frm/thread/9aa0666ca349e4ba?hl=en_US.

From the name you can get an ExcelReference and hence the sheetId
using
ExcelReference sheetRef =
(ExcelReference)XlCall.Excel(XlCall.xlSheetId, sheetName);

I think the sheetIds will be stable for a session, but you should test
this.

Regards,
Govert

Sam.Park

unread,
Jun 8, 2011, 1:33:52 AM6/8/11
to Excel-DNA
Thanks for reply Govert

I can get all of sheet ids below codes. And I confirmed the sheetid
wouldn't be changed

for a session even if i changed the sheet order or sheet name. Thanks
for your support

object result = XlCall.Excel(XlCall.xlfGetWorkbook, 16);
string workbookname = result.ToString();
object[,] sheetnames =
(object[,])XlCall.Excel(XlCall.xlfGetWorkbook, 1, workbookname);

string[] strsheetnames = new string[sheetnames.GetLength(1)];

for (int j = 0; j < strsheetnames.Length; j++)
{
strsheetnames[j] = sheetnames[0,
j].ToString().Split(']').LastOrDefault();
}

ExcelReference sheetRef =
(ExcelReference)XlCall.Excel(XlCall.xlSheetId, strsheetnames[0]);

Thanks
Park
On 6월7일, 오후3시40분, Govert van Drimmelen <gov...@icon.co.za> wrote:
> Hi Park,
>
> You can get all the sheet names in a book with the C API using the
> GET.WORKBOOK function (xlfGetWorkbook). There was more on using the
> Get.XXX information functions in this discussion:http://groups.google.com/group/exceldna/browse_frm/thread/9aa0666ca34....
Reply all
Reply to author
Forward
0 new messages