I did like this:
if (Excel._Application.ActiveWorkbook != null)
{
List sheets = new List();
foreach
In VSTO, the CodeName
property is an infra-structure property that you should not be using from your code.
From MSDN:
This property supports the Visual Studio Tools for Office infrastructure 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.
Also, I noted from your code that you are using an Excel Addin. You can try to check if the CodeName
property returns what you expect if you use an Excel Document Customization instead of an Excel Addin.
Update: In order for you to uniquely tag a worksheet you could use a GUID and set it as a custom property of the worksheet using Worksheet.CustomProperties.