Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...just the few hours I've spent on this site I have learned a lot. I can now implement features that will be very useful to my company, and some of those I never knew existed..."

Geography

Where in the world do Tek-Tips members come from?

parsing data from xlsx file?

Ouka (TechnicalUser)
23 Apr 12 14:10
Hi all,

Stupid, basic question I am sure, but I am having trouble having a VBScript file open & access data from an xlsx file.  My method works fine for .txt and .csv files, but not sure on the syntax needed for .xlsx files.



set FSO_source = createobject("Excel.Application")
set source_file = FSO_source.workbooks.open("C:\Data\file.xlsx", 1)

source_content = source_file.ReadAll  'trouble is here
parse_content = split(source_content, " ")
count = parse_content(2)

msgbox count

the excel object doesn't seem to like .ReadAll, tho it seems to work fine for .csv and .txt files

Essentially what I am trying to do is get 3rd word of the source document, located in A1 if opened in excel.

Thanks in advance!
-o
 
SkipVought (Programmer)
23 Apr 12 14:21


your .xls* files are NOT text files.

you must either use creatobject to create an excel application object and open the excel file as an excle file, or have excel saveas a .csv in a prior processs and then you can access this text file.

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Ouka (TechnicalUser)
23 Apr 12 14:40
Isn't that what I am doing in my first two lines?  The file is opening fine, I just apparently don't know the syntax for reading the data once it is open.

saving as csv really isn't an option, since downstream scientific machine processes require the file in xlsx format.
SkipVought (Programmer)
23 Apr 12 14:46


you cannot use a text function to read an excel file.

You have OBJECTS and other stuff in an excel file

You must reference a worksheet and in the worksheet a range.

what is it that you want to access in this workbook?

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Ouka (TechnicalUser)
23 Apr 12 16:07
ah.  i wasn't aware i could use the standard excel object hierarchy directly from a vbs script.  handy that.

replacing

source_content = source_file.ReadAll function

with

source_content = source_file.activesheet.cells(1,1).value

seemed to work fine.

I guess my worry is that if something changes in the upstream processes that somehow moves the data I'm trying to get out of A1, then the vbs I'm writing will cease to function.

Is there an excel function equivalent to the .Readall txt function, or would I have to write some sort of looping structure to look at each cell individually to look for cells that contain data LIKE the data I'm looking for, then parse those strings for the particular data I need?
SkipVought (Programmer)
23 Apr 12 16:14


CODE

dim a as variant, r as long, c as integer

a = source_file.worksheets(1).usedrange

for r = 0 to ubound(a, 1)
  for c = 0 to ubound(a, 2)
     msgbox a(r,c)
  next
next
usedrange is the range containing DATA.

DATA can be values and/or OTHER RANGE PROPERTIES. So a could contain null elements

Skip,

glassesJust traded in my old subtlety...
for a NUANCE!tongue

Ouka (TechnicalUser)
23 Apr 12 18:14
Ah, thank you very much.  Much cleaner than how I was trying to manage it.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close