Solved

Vb.Net - Excel 2013 Invalid Index

Posted on 2014-09-12
7
620 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
ID: 40319777
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
ID: 40323513
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
ID: 40323683
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jimbo99999
ID: 40325315
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
ID: 40326440
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
ID: 40327966
Hello ChloesDad:

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

Thanks,
jimbo99999
0
 

Author Closing Comment

by:Jimbo99999
ID: 40400486
Thanks you...it is working now!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SqlServer no dupes 25 41
Write to a printer using vb.net 9 57
Help with opening combobox using VB.NET. 3 34
ASP.NET (VB) return a record 2 51
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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