Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to program Autofilter on with Excel Interop 1

Status
Not open for further replies.

AndyH1

Programmer
Joined
Jan 11, 2004
Messages
350
Location
GB
I'm generating Excel using the Excel 11 Object Library as in:


It works fine and I can generate an Excel report with the titles formatted as black on grey.

The customer wants AutoFilter set as on by default though, and I can't seem to find how to do this.

Iv'e tried 'xlWorkSheet.AutoFilter = True' but I get an error saying this is read only, so this is obviously not the way to assign the value just determine it.

xlWorkSheet.Range("$A1").AutoFilter() seems the most likely way to set Iv'e found but this gives System.Runtime.InteropServices.COMException: AutoFilter method of Range class failed and I can't determine why. Iv'e tried various types of ranges such as ("$A1:$IV1") but no joy.

Can anyone advise on this?
Also if anyone knows a good reference for this sort of programming (VB.Net generation of Excel) would be grateful

Thanks
Andy
 
Try this:

xlWorkSheet.Range("$A1").Select()
xlWorkSheet.Application.Selection.AutoFilter()

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
The easist thing to do when working with an Excel is to just record a macro. When working with an Excel 11 Object the commands are almost exactly the same.

This:
Code:
'
'
' Macro1 Macro
' Macro recorded 8/19/2008 by User
'

'
    Range("A1:D8").Select
    Selection.AutoFilter

Becomes:
Code:
xlWorkSheet.Range("A1:D8").Select()
xlWorkSheet.Application.Selection.AutoFilter()
Like jebenson said.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Ooopsie.
Code:
xlWorkSheet.Application.Selection.AutoFilter()
Should only need to be.
Code:
xlWorkSheet.Selection.AutoFilter()

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
No it is application. I should have double checked before I said.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Because you do Sheet1.Application.Selection.AutoFilter in Excel.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks jebenson for solving it, and Sorwen for the suggestion of using Macros to figure out what to do. Was tearing what little hair I have out trying to solve this.

many thanks
Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top