简体   繁体   中英

Saving CustomProperty in Excel Worksheet

We have Excel add-in in C#. To support some features we are using Worksheet.CustomProperties . We found that after adding some custom property we are changing its value. For example we set "1" via Add method. At some point we change the same value to "2". Then we save workbook and open it again. For some reason we see "1" instead of "2". Why? It looks I missed something, but don't know what. Updated:

    public class CustomPropertyUtil
{
    protected readonly Worksheet Sheet;

    public WorkSheetCustomPropertyUtil(Worksheet sheet)
    {
        this.Sheet = sheet;
    }

    protected bool Exists(string propertyName)
    {
        try
        {
            return Sheet.CustomProperties.Cast<CustomProperty>().Any(c => c.Name == propertyName);
        }
        catch (System.Runtime.InteropServices.COMException)
        {
            return false;
        }
    }

    protected object GetValue(string propertyName)
    {
        CustomProperty property = GetProperty(propertyName);
        return property != null ? property.Value : null;
    }

    protected void SetValue(string propertyName, object value)
    {
        CustomProperty customProperty = GetProperty(propertyName);

        if (customProperty == null)
        {
            AddCustomProperty(propertyName, value);
        }
        else
        {
            customProperty.Value = value;
        }

    }

    private void AddCustomProperty(string propertyName, object value)
    {
        Sheet.CustomProperties.Add(propertyName, value);
    }

    protected CustomProperty GetProperty(string propertyName)
    {
        return Exists(propertyName)
            ? Sheet.CustomProperties.Cast<CustomProperty>().FirstOrDefault(c => c.Name == propertyName)
            : null;
    }
}

This is how we manage custom properties. When we save we do following:

Workbook.SaveAs(filePath, AccessMode: XlSaveAsAccessMode.xlNoChange);

When I open it in VBA I see that my CustomProperties were not saved.

Maybe I've oversimplified your task, but I set custom properties (for example, attributes when the file is saved to a Sharepoint document library) as follows:

Excel.Workbook wb;

wb.ContentTypeProperties["Region"].Value = "Northeast";

It's possible these are different properties than the ones you are talking about... this will change the properties, for example, when you click on the "File" tab and see the "Properties" list in the right-most panel.

This answer is probably six years too late, but here's some example code for working with CustomProperties and CustomProperty of WorkSheet objects, including a function to get them by the .Name property.

I'm using Excel 2019, and the CustomProperties collection and any CustomProperty objects in it do get saved with the workbook. I just tested this by saving, closing, and reopening, and the CustomProperty values were retained.

Public Sub CustomPropertyTest()
    
    Dim xl          As Excel.Application
    Dim ws          As Excel.Worksheet
    Dim wb          As Excel.Workbook
    Dim cps         As Excel.CustomProperties
    Dim cp          As Excel.CustomProperty
    
    Set xl = Excel.Application
    Set wb = xl.ActiveWorkbook
    Set ws = wb.ActiveSheet
    Set cps = ws.CustomProperties
    
    If CustomPropertyByName("Test1") Is Nothing Then
        Set cp = cps.Add("Test1", "Hello!")
    End If
    If CustomPropertyByName("Test2", ws) Is Nothing Then
        Set cp = cps.Add("Test2", "Hello again!")
    End If
    
    Set cp = CustomPropertyByName("Test1", ws)
    'This can also be used if you're certain of the item index.
    'Set cp = cps.Item(1)
        
    Stop
    
    'Change the value.
    cp.Value = "2"
    
    Stop
    
    'Change the value again.
    cp.Value = "3"
   
    Stop
    
End Sub

Public Function CustomPropertyByName( _
                    ByVal cpName As String, _
                    Optional ByRef wsObj As Excel.Worksheet _
                    ) As CustomProperty
                    
    Dim cps         As Excel.CustomProperties
    Dim cp          As Excel.CustomProperty
    
    If wsObj Is Nothing Then
        Set wsObj = Excel.ActiveSheet
    End If
    
    Set cps = wsObj.CustomProperties
    For Each cp In cps
        If cp.Name = cpName Then
            Set CustomPropertyByName = cp
            Exit Function
        End If
    Next
    
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM