Advanced filter in PowerShell

后端 未结 1 1710
悲哀的现实
悲哀的现实 2021-01-06 18:55

I am trying to use the Excel advanced filter through PowerShell, but I am not having any luck. I can use an autofilter successfully by running the following code:

         


        
1条回答
  •  孤城傲影
    2021-01-06 19:06

    None of the arguments to AdvancedFilter() is a string.

    Object AdvancedFilter(
        XlFilterAction Action,
        Object CriteriaRange,
        Object CopyToRange,
        Object Unique
    )
    

    The first is an enumeration. In VBA you can use those directly because there they are implicitly global. Not so in Powershell, where you have to reference them explicitly by their fully qualified names:

    $xlFilterInPlace = [Microsoft.Office.Interop.Excel.XlFilterAction]::xlFilterInPlace
    

    The other three arguments are typed as Object, which means they are of Variant type in COM. However, #2 and #3 are supposed to be Range objects, all bets are off if you pass in something else.

    They are also marked as optional. Optional parameters that should have no value are represented by the Missing type in .NET COM Interop. Again, in Powershell you have to reference it explicitly:

    $missing = [Type]::Missing
    

    Argument #4 is supposed to be a Boolean, so just pass a Powershell bool constant (or, since this parameter is optional as well, $missing).

    $excel.Selection.AdvancedFilter($xlFilterInPlace, $missing, $missing, $TRUE)
    

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