Solved

Vb.Net - Excel 2013 Invalid Index

Posted on 2014-09-12
7
516 Views
Last Modified: 2014-10-23
Good Day Experts!

I am in a bit of a quandry here.  I have a VB.Net application with a TabControl that has multiple "Export to Excel" buttons on the Tabs.  It has worked fine for 2years on XP machines and recently Windows7 machines.  

However, we just upgraded a User to Office 2013 and now I am having trouble when trying to "Export to Excel".  Message received --> Invalid Index. (Exception from HRESULT:0x8002000B (DISP_E_BADINDEX)).  When I go to my machine which is Windows7 and Office 2010 I have no troubles.

Perhaps I need to create the objects a bit different for 2013 but I am not sure?

Do you have any ideas?

Thanks,
jimbo99999
0
Comment
Question by:Jimbo99999
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
We would need more information such as a stack trace to see the line of code that is causing the exception. and the source code at that point.
0
 

Author Comment

by:Jimbo99999
Comment Utility
Good Day Everyone!

I put some message boxes in the code by User so as to not disturb the other Users when I published.  I have narrowed it down to the line causing the error with the Office 2013 W7 machine and have bolded it below.  

Note, that this code works fine on my Office 2010 W7 machine.  

 oWB = oXL.Workbooks.Add
 oSheet = oWB.ActiveSheet
 If rbtnCashReceipts.Checked = True Then
          oSheet.Name = "Funding Report"
 Else
          oSheet.Name = "Pymt Release Report"
 End If
oSheet2 = oWB.Worksheets(2)
oSheet2.Name = "Check Details"
oSheet3 = oWB.Worksheets(3)
oSheet3.Name = "Invoice Details"

Is there a way to reference the worksheet that will work for both versions?

Thanks,
jimbo99999
0
 
LVL 15

Expert Comment

by:ChloesDad
Comment Utility
The error is caused by there not being 3 worksheets in the spreadsheet. You could check using the Count property and then add the extra sheets if necessary.

NumberOfSheets = oWB.Count

IF NumberOfSheets >= 3 then
  oSheet2 = oWB.Worksheets(2)
ELSE
 oSheet2 = CType(oWB.Add(), Excel.Worksheet)
 NumberOfSheets =NumberOfSheets + 1
END IF

oSheet2.Name = "Check Details"

IF NumberOfSheets >= 4 then
  oSheet3 = oWB.Worksheets(3)
ELSE
 oSheet3 = CType(oWB.Add(), Excel.Worksheet)
 NumberOfSheets =NumberOfSheets + 1
END IF

oSheet3.Name = "Invoice Details"
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Jimbo99999
Comment Utility
Good Day!

I apologize as I did not include my declaration block...here it is:

        Dim oXL As Excel.Application
        Dim oWB As Excel.Workbook
        Dim oSheet As Excel.Worksheet
        Dim oSheet2 As Excel.Worksheet
        Dim oSheet3 As Excel.Worksheet
0
 
LVL 15

Accepted Solution

by:
ChloesDad earned 500 total points
Comment Utility
Thats fine, but if there are not enough worksheets in the active workbook then you will get the error that you are seeing.

Just because you have sheets declared as objects doesn't mean that they are actually present in the workbook, they just exist as objects in the subroutine.

In excel you can set the number of sheets in the workbook that are created by default. This is probably what is different on the machine that has a problem.
0
 

Author Comment

by:Jimbo99999
Comment Utility
Hello ChloesDad:

Ahhh...I gotcha.  Ok, I will check that out today.

Thanks,
jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
Comment Utility
Thanks you...it is working now!!!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now