Solved

Excel 2003 vba - readline from xls

Posted on 2010-08-27
6
1,320 Views
Last Modified: 2012-05-10
hi,
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?
0
Comment
Question by:jamiepryer
6 Comments
 
LVL 10

Accepted Solution

by:
borgunit earned 500 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

'------------------------------------------------------------------------------

'OpenExcelWorksheet:

'

'------------------------------------------------------------------------------

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

0
 
LVL 5

Expert Comment

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

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

Expert Comment

by:Patrick Matthews
ID: 33543677
INHOUSERES,

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.

Patrick
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 33

Expert Comment

by:Norie
ID: 33549268
jamie

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".
0
 

Author Comment

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

will have a look at the replies tomorrow, thanks
0
 
LVL 5

Expert Comment

by:INHOUSERES
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

911 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

19 Experts available now in Live!

Get 1:1 Help Now