• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 172
  • Last Modified:

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.
0
DEIIT
Asked:
DEIIT
  • 2
2 Solutions
 
Arthur_WoodCommented:
try this change, from:

excelSheet = excelBook.Sheets.Add


to:

excelSheet = excelBook.Sheets.Add(After:=Worksheets(Worksheets.Count))


That will add the new shett AFTER the last sheet in the current set.

AW
0
 
bramsquadCommented:
add the sheet, then select the sheet

replace this

excelSheet = excelBook.Sheets.Add

with this

excelBook.Worksheets.Add()
excelSheet = excelBook.Worksheets.Item(SheetIndexNumber)

~b
0
 
bramsquadCommented:
also note the difference in the code - using 'worksheet' instead of 'sheet'
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now