Solved

Problem reading some excel files

Posted on 2007-03-19
7
1,336 Views
Last Modified: 2008-03-03
Hello,

I am reading excel files as follow;

 Dim oXL As excel.Application
        Dim oBook As excel.Workbook
        Dim oSheet As excel.Worksheet

        'create app    
        oXL = CreateObject("Excel.Application")
        oXL.DisplayAlerts = False

        'creat workbook
        oBook = oXL.Workbooks.Open("C:\070206_shb_rh_s05_activité_CG100.xls", Type.Missing, True, Type.Missing, Type.Missing, Type.Missing, False, Type.Missing, Type.Missing, Type.Missing, False, Type.Missing, Type.Missing, Type.Missing, Type.Missing)

        'selecting sheets/renaming sheets
        oBook.Worksheets("BALANCE AGEE").Select()
        oSheet = oBook.Worksheets("BALANCE AGEE")

        Dim str As String = oSheet.Range("B6").Value

For some files its fine. But for others, I get an error at this line:
        oBook.Worksheets("BALANCE AGEE").Select()

For the files I am having problems,  I get a warning stating that the workbook contains external links. It asks whether it shoold update or ignore. No matter my choice i get:


[COMException (0x8002000b): Index non valide. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))]
   Microsoft.Office.Interop.Excel.Sheets.get__Default(Object Index) +0
   test2.Page_Load(Object sender, EventArgs e) in C:\Documents and Settings\BABA\Mes documents\Visual Studio 2005\sifca\test2.aspx.vb:27
   System.Web.UI.Control.OnLoad(EventArgs e) +88
   System.Web.UI.Control.LoadRecursive() +74
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3035

Can someone  help me on this?

 


0
Comment
Question by:bkourouma
  • 4
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 18753543
think line:

oBook.Worksheets("BALANCE AGEE").Select()

can be omitted in your case here, what if you removed/remarked this line?
0
 
LVL 1

Author Comment

by:bkourouma
ID: 18754502
ryancys,
I still get the error.

Also I need to make sure that annoying warning does not come up.

Thanks.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 18754561
Can you make sure every excel documents got a worksheet called as "BALANCE AGEE" ?
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:bkourouma
ID: 18754585
Yes, it does. The document I am reading does have ."BALANCE AGEE"

As matter of fact, here is the actual doc  http://www.allianceconsultants.net/070206_shb_rh_s05_activité_CG100.xls

Thanks for your time.
0
 
LVL 1

Author Comment

by:bkourouma
ID: 18757440
Thanks, the problem was indeed the name in the sheet contained empty string as in ."BALANCE AGEE  ".
Is there a way to rename the sheet  "BALANCE AGEE"?

Thanks


0
 
LVL 51

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 18761209
try replace:

oSheet = oBook.Worksheets("BALANCE AGEE")

to:

oSheet = oBook.Worksheets(1)


To trim a worksheet's name, try like:

oSheet.Name = Trim(oSheet.Name)
0
 
LVL 1

Author Comment

by:bkourouma
ID: 18762813
Thanks, I tried that but i was doing oSheet = oBook.Worksheets(0) for the first sheet. lol
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add a task in Outlook from access 11 42
Spell Check in VB6 13 127
RUNRMTCMD from AS/400 12 71
VB script to continue despite error 2 17
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 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