Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Excel Automation : (1) Print options (2) Active worksheet

Jul 19, 2005 1:07AM PDT

I'm a new programmer, and have had to jump into Excel COM automation already, through C#. I'm finding that most of the examples over the web are in VB, but I've managed to adapt them to C#. I've also gotten help by writing macros in Excel then trying to duplicate the code through the Interop assembly. Here are my questions:

(1) How can I set the default print settings for printing the entire workbook (not just the active sheet)? The only thing I've encountered along this line is the PrintOut method, but that seems to print the application as soon as it is read in the code. I want to wait until the user prints the workbook himself, because once the Excel application is loaded, I'm turning it over to the user. Is there a way to preset the Print Entire Workbook option in the print settings without asking the user to do it?

(2) I'm trying to freeze the first row (column headers) for each worksheet in a workbook. I read how to do that for one worksheet (at this site) and managed to implement the code for all the worksheets using a for each loop:

foreach (Excel.Worksheet MySheet in MyWorkbook.Worksheets)
{
//Activate each sheet in the workbook so the ...ActiveWindow.SplitRow
//and ...ActiveWindow.FreezePanes methods can be applied to each sheet, not just one.

TraceSheet.Activate();

//Freeze the column headers for each sheet. TraceSheet.Application.ActiveWindow.SplitRow = 1; TraceSheet.Application.ActiveWindow.FreezePane s = true;
}
Is this the best way? What is the active sheet, and can you have more than one? My problem is that I want the first sheet to be selected when the workbook loads, but I can't find a Select() method, and Activate() is a get method only. Any help will be appreciated.

Thanks,

Oliver B

Discussion is locked