Hi Georg,
If you have a reference to the right version of the Primary Interop
Assembly (for Excel 2010 it's version 14.0), I don't think there is a
problem with the AutoFilter command. The documentation you point to
might be misleading - that version of the PIA does indeed have all the
options for XlAutoFilterOperator, and the xlFilterValue operator works
fine.
In terms of access to the COM API you are in the same position with
Excel-DNA as with any other .NET-based add-in project (which is the
same access via COM you have from VBA too). Excel-DNA additionally
gives you access to the Excel C API, but that is not helpful in this
case since the C API has not been updated with new features like the
ListObject/Tables from recent versions.
So you should be able to "use the full Excel 2010 functionality"
equivalently from VBA, VSTO or Excel-DNA. On top of that, Excel-DNA
gives you another option - the C API.
I tried the following, to explore the API and make sure the AutoFilter
works as expected. Then I used the .NET 4 dynamic support to make the
COM calls, and finally the PIA assemblies.
1. VBA
------
Make a sheet with two columns, with the first column including some
values like "A" and "E":
Left Right
A 1
B 2
C 3
D 1
E 2
A 3
B 1
C 3
D 4
Select the data and insert a table (with headers)
Create a VBA subroutine:
Sub ApplyFilter()
ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1,
Criteria1:= _
Array("A", "B", "E"), Operator:=xlFilterValues
End Sub
Run with F5 and check that it worked as expected.
2. Excel-DNA with C# 4 dynamic COM
----------------------------------
Copy ExcelDna.xll from the distribution into a folder, and rename to
AutoFilter.xll
Create a new text file in the same directory, called AutoFilter.dna
<DnaLibrary RuntimeVersion="v4.0" Language="C#" >
<![CDATA[
using System;
using ExcelDna.Integration;
public static class Test
{
[ExcelCommand(MenuText="DoAutoFilter")]
public static void DoAutoFilter()
{
// From docs or VBA Object Browser
int xlFilterValues = 7;
object[] criteria = new[] {"A", "B", "E"};
dynamic app = ExcelDnaUtil.Application;
app.ActiveSheet.ListObjects["Table1"]
.Range.AutoFilter(
Field: 1,
Criteria1: criteria,
Operator: xlFilterValues);
}
}
]]>
</DnaLibrary>
This creates a menu item (under the Add-Ins tab) that does the
filtering.
Note I'm using the 'dynamic' support and option arguments feature
from .NET 4. This means the COM call is late-bound.
All works as expected.
3. Excel-DNA with the Excel Primary Interop Assembly (PIA)
----------------------------------------------------------
<DnaLibrary RuntimeVersion="v4.0" Language="C#" >
<Reference Name="Microsoft.Office.Interop.Excel" />
<![CDATA[
using System;
using Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
public static class Test
{
[ExcelCommand(MenuText="DoAutoFilter")]
public static void DoAutoFilter()
{
// From docs or VBA Object Browser
object[] criteria = new[] {"A", "B", "E"};
Application app = (Application)ExcelDnaUtil.Application;
Worksheet activeSheet = (Worksheet)app.ActiveSheet;
activeSheet.ListObjects["Table1"].Range.AutoFilter(
Field: 1,
Criteria1: criteria,
Operator: XlAutoFilterOperator.xlFilterValues);
}
}
]]>
</DnaLibrary>
4. Excel-DNA with the Excel PIA - .NET 2.0 syntax
-------------------------------------------------
<DnaLibrary Language="C#" >
<Reference Name="Microsoft.Office.Interop.Excel" />
<![CDATA[
using System;
using Microsoft.Office.Interop.Excel;
using ExcelDna.Integration;
public static class Test
{
[ExcelCommand(MenuText="DoAutoFilter")]
public static void DoAutoFilter()
{
// From docs or VBA Object Browser
object[] criteria = new object [] {"A", "B", "E"};
Application app = (Application)ExcelDnaUtil.Application;
Worksheet activeSheet = (Worksheet)app.ActiveSheet;
activeSheet.ListObjects["Table1"].Range
.AutoFilter(1, criteria,
XlAutoFilterOperator.xlFilterValues,
null, null);
}
}
]]>
</DnaLibrary>
All of this can of course be done in your Visual Studio project too,
and can also be triggered from a Ribbon button, a shortcut key or an
event.
If I need to explain any of this in more detail, please ask.
Regards,
Govert
On Jun 5, 11:22 am, georg <
g.wi...@bluewin.ch> wrote:
> Hi Govert
>
> Thanks for the quick reply.
>
> 1. I will follow up on your pointer concerning the caching issue.
>
> 2. Concerning Microsoft.Office.Interop.Excel.Range.AutoFilter:
> The Excel 2010 Primary Interop Assembly documentation for the
> Range.AutoFilter method at:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.exce...