Excel 2003 vba - readline from xls

Posted on 2010-08-27
Medium Priority
Last Modified: 2012-05-10
ive got some code for reading in files via a macro written in excel 2003.
The code works really well on .txt. or .csv files

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objFile = objFso.OpenTextFile("c:\balh.txt)
strLine = objFile.ReadLine
arrStrLine = split(strline,vbtab)

however, when i try and read in an .xks file, it goes wrong and the line comes out with smething like this:
strLine: ""ÐÏࡱá>þÿ      ,°þÿÿÿbãdåfçhéjëlínïpñrót      õ      v""

is this because you cant use this method to read in an xls file?
is there another way to read xls files?
Question by:jamiepryer
LVL 10

Accepted Solution

borgunit earned 2000 total points
ID: 33540967
You need to open it as an object. You cannot read it line by line as a text file (different format). I hope this helps.
Public Function OpenExcelWorksheet(ByVal sPath As String, ByVal sWorkSht As _
String, Optional bVisible As Boolean = False, Optional bReadOnly As Boolean = _
False) As Excel.Worksheet
Dim xlApp As Excel.Application
Dim xlWorkbook As Excel.Workbook
On Error Resume Next

If sPath = "" Then
    MsgBox "No Excel File Specified"
End If
Set xlApp = CreateObject("Excel.Application")
If Err Then
    MsgBox "Cannot start Excel", vbCritical, "Error Starting Excel"
'Unload Me
End If
If bVisible Then
    xlApp.Visible = True
End If

Set xlWorkbook = xlApp.Workbooks.Open(sPath, , bReadOnly)
Set OpenExcelWorksheet = xlApp.Worksheets(sWorkSht)
End Function

Open in new window


Expert Comment

ID: 33541148
borgunit, your suggestion uses early and late binding.

just change the dims to "as Object" rather than "excel.xxxx"
LVL 93

Expert Comment

by:Patrick Matthews
ID: 33543677

Given that the Asker has stated that his/her code was written to be executed in Excel 2003, then I see nothing wrong at all with borgunit's variable declarations.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

LVL 37

Expert Comment

ID: 33549268

Could the clue be in 'OpenTextFile'?

An Excel file is not a text file, though like any file you could treat it like one - but you'll just end up
with the gobblyedgook you are seeing.

What is it you are trying to do?

The easiest way to work with an Excel file is to open it as one using something like Workbooks.Open "C:\TheExcelFile.xls".

Author Comment

ID: 33569594
Could the clue be in 'OpenTextFile'? - good point! cheers

will have a look at the replies tomorrow, thanks

Expert Comment

ID: 33574425
matthewspatrick - I agree... I actually put it the wrong way round.

He's used early binding in the dims, and also doesn't really need to prefix it with "excel."
Then he uses late binding when setting the objects.

It would work, but there are neater ways to do it, which may be easier to read through for newer users of VBA and macro's etc.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

619 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