Search code examples
excelvstooffice-interopoffice-addinsexcel-addins

C# Excel VSTO save a Filter in Sheet View


It´s possible to save a filter in Excel using COM API? This feature is very useful if you have someone else working with the same excel to have a different filter for each user.

enter image description here

I know there is a new Interfaces to do this job:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.office.interop.excel.sheetviews?view=excel-pia


Solution

  • You need to enumerate the SheetViews collection that is a member of Application.ActiveWindow.

    Application app = /*your_Excel_app*/;
    
    var activeWindow = app.ActiveWindow;
    
    foreach (WorksheetView view in activeWindow.SheetViews)
    {
        // do something
    }