How to get the CodeName for Worksheet in Excel using VSTO

后端 未结 2 1960
南方客
南方客 2021-01-20 01:30

I did like this:

if (Excel._Application.ActiveWorkbook != null)
{
    List sheets = new List();
    foreach         


        
相关标签:
2条回答
  • 2021-01-20 01:56

    In your condition, you can use Worksheet.CustomProperties as alternative to hold unique property of the sheet.

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

    So, later on 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.

    0 讨论(0)
  • 2021-01-20 02:00

    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.

    0 讨论(0)
提交回复
热议问题