Link to home
Start Free TrialLog in
Avatar of DEIIT
DEIIT

asked on

Problems adding new spreadsheet to an existing Excel file

I keep receiving the following error whenever I try to add a new Spread Sheet to an existing Excel File.
Exception from HRESULT: 0x800A03EC.
         at Excel.Sheets.Add(Object Before, Object After, Object Count, Object Type)
         at CamShaft.Form1.AddNewExcelSheet() in <My Program Name>
Here is my code:

Dim excelApp As Excel.Application
Dim excelBook As Excel.Workbook
Dim excelBook2 As Excel.Workbook
Dim excelSheet As Excel.Worksheet

excelApp = New Excel.Application
If TempType.Text = "T" Then
   excelBook = excelApp.Workbooks.Open(TestFile.Text)
ElseIf TempType.Text = "R" Then
   excelBook = excelApp.Workbooks.Open(RealFile.Text)
End If
excelSheet = excelBook.Sheets.Add
excelSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape

Any help would be appreciated.
ASKER CERTIFIED SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
also note the difference in the code - using 'worksheet' instead of 'sheet'