Excel Automation

U

Ugo

Hi,
Can anyone help? I am creating an Excel file in asp.net and when
I set the PageSetup.Orientation I get the following error.

Unable to set the Orientation property of the PageSetup class
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Unable to
set the Orientation property of the PageSetup class




oXL = new Excel.Application();

oXL.Visible = false;

//Get a new workbook.

oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));

oSheet = (Excel._Worksheet)oWB.ActiveSheet;


oSheet.PageSetup.Orientation= Excel.XlPageOrientation.xlLandscape;

oSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLegal;





Thanks,

Ugo
 
B

Beeeeeves

Don't know. can you run the exact same (equivalent) code in Excel VBA?

BUT:
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));

you say you're using ASP.NET - you do realise your Excel process will never
die when you do this??!! Every time this operation is executed, a new Excel
will be created and never die, they will just build up on your web server
unless you kill them - to do this you need to set EVERY reference to a COM
object that you've retrieved from Excel to an explicit variable, in order to
call ReleaseComObject on them ALL, and then set them to null, at the end.
This *includes* the 'workbooks' collection, which you're currently not
assigning to a variable. The workbooks object might be a collection, but
it's still a COM object in its own right and will get lost in the CCW if you
don't explicitly free it. There's a KB article about it somewhere but I
can't remember its number.
 
A

Arne Janning

Ugo said:
Hi,
Can anyone help? I am creating an Excel file in asp.net and when
I set the PageSetup.Orientation I get the following error.

Unable to set the Orientation property of the PageSetup class
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Unable to
set the Orientation property of the PageSetup class

oXL = new Excel.Application();
oXL.Visible = false;
//Get a new workbook.
oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
oSheet.PageSetup.Orientation= Excel.XlPageOrientation.xlLandscape;
oSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperLegal;

Thanks,

Ugo

Hi Ugo,

first question: which Excel-Version? PIAs installed or not?

the following code works for me (Excel 2003, PIAs installed)

using Microsoft.Office.Interop.Excel;

private void button1_Click(object sender, System.EventArgs e)
{
object miss = Type.Missing;
ApplicationClass oXL = new ApplicationClass();
oXL.Visible = false;
//Get a new workbook.
Workbook oWB = (Workbook) oXL.Workbooks.Add(miss);
oXL.Visible = true;

Worksheet oSheet = (Worksheet) oWB.ActiveSheet;
oSheet.PageSetup.Orientation= XlPageOrientation.xlLandscape;
oSheet.PageSetup.PaperSize = XlPaperSize.xlPaperLegal;
}

Depending on your version and PIAs we probably have to adapt the code.

Cheers

Arne Janning
 
A

Arne Janning

Beeeeeves said:
Don't know. can you run the exact same (equivalent) code in Excel VBA?

BUT:




you say you're using ASP.NET - you do realise your Excel process will never
die when you do this??!! Every time this operation is executed, a new Excel
will be created and never die, they will just build up on your web server
unless you kill them - to do this you need to set EVERY reference to a COM
object that you've retrieved from Excel to an explicit variable, in order to
call ReleaseComObject on them ALL, and then set them to null, at the end.

How do you know that Ulo is not aware of this? Perhaps he just posted
sample code. He might release the objects somewhere else.
This *includes* the 'workbooks' collection, which you're currently not
assigning to a variable. The workbooks object might be a collection, but
it's still a COM object in its own right and will get lost in the CCW if you
don't explicitly free it.

??? Could you please explain this in detail?
There's a KB article about it somewhere but I
can't remember its number.

Please search for it. I would be very interested.

Cheers

Arne Janning
 
G

Guest

How do you know that Ulo is not aware of this?

I don't - but he didn't appear to be assigning the Workbooks collection to anything.

Perhaps he just posted
sample code. He might release the objects somewhere else.

All is well then
??? Could you please explain this in detail?

That IS detail... I can't really say it any simpler than "The Workbooks collection IS a COM object in its own right"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top