How to get CodeName for worksheet in Excel using VSTO

I like it:

if (Excel._Application.ActiveWorkbook != null) { List<WorksheetKeyValue> sheets = new List<WorksheetKeyValue>(); foreach (object ws in ExcelApp.ActiveWorkbook.Worksheets) { string strCodeName = ws.CodeName } } 

but strCodeName is an empty string if it should be Sheet1, Sheet2, ..., SheetN, as in VBA.

thanks

+4
source share
2 answers

In your state, you can use Worksheet.CustomProperties as an alternative to store a unique sheet property.

 Worksheet ws = **current_sheet** as Worksheet; ws.CustomProperties.Add("SheetID", **some_value**); 

So later you can access them as

 foreach (Excel.CustomProperty prop in ws.CustomProperties) { if (prop.Name == "SheetID") { // access as prop.Value and prop.Name } } 

Hope this helps.

+5
source

In VSTO, the CodeName property is an infrastructure property that you should not use from your code.

From MSDN:

This property supports Visual Studio Office Infrastructure Tools and is not intended to be used directly from your code.

Tell us what you are trying to accomplish, maybe there is an alternative way to do what you want.

In addition, I noted from your code that you are using Excel Addin. You can check if the CodeName property CodeName what you expect if you use Excel Document Customization instead of Excel Addin.

Update: So that you can clearly label the worksheet, you can use the GUID and set it as a custom property of the worksheet using Worksheet.CustomProperties .

+2
source

Source: https://habr.com/ru/post/1344746/


All Articles