Name conflicts with _FilterDatabase

ezcoding picture ezcoding · Feb 1, 2013 · Viewed 23.1k times · Source

I'm getting a "Name conflicts" dialog box after I try to run my Powershell script the second time on the same file. I know that this error has something to do with me autofiltering the file. If I never use autofiter, then the dialog box won't show. What am I doing wrong? Here's the code:

$excel = new-object -comobject excel.application
$excel.DisplayAlerts = $false

$workbook = $excel.Workbooks.open("testtabelle1.xlsx")
Trap {"Worksheet problems..."} $workbook.Worksheets.Item(1) | Out-Null

$from = ">0"
$to = "<2"

$workbook.ActiveSheet.Range("D:D").AutoFilter(1, $from, 1, $to) > $null

#Reset Selection and close file
#$workbook.ActiveSheet.Range("A1").Select() | Out-Null
$workbook.Save()
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)

Answer

Daniel picture Daniel · Feb 1, 2013

You have to delete the Name "_FilterDatabase", because this name is only allowed once.

The Names object is a hastable. Therefore you Need to delete the key like this:

$workbook.Names.Item("_FilterDatabase").Delete()