Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Read from Excel using VBScript

Posted on 2006-11-03
4
Medium Priority
?
20,371 Views
Last Modified: 2008-03-10
I use the following VBScript function to read data from Excel.
It works fine, but I have to specify the spreadsheet name (sSQL = "SELECT * FROM [" & sSheetName & "$]").
Is there a way to read from Excel without specifying spreadsheet name?

For example now I call the function

ReadDateFromExcel("c:\myfile.xls", "Sheet1")

I'd like to omit the spreadsheet name (Sheet1) and just read from the first spreadsheet in Excel document, regardless what it's named

ReadDateFromExcel("c:\myfile.xls")



Function ReadDataFromExcel(sFileNameAndPath, sSheetName)
      Dim conn
      Set conn = CreateObject("ADODB.Connection")
      With conn
            .Provider = "Microsoft.Jet.OLEDB.4.0"
            .ConnectionString = _
          "Data Source=" & sFileNameAndPath & ";" & _
          "Extended Properties=Excel 8.0;"
            .Open
      End With
      Dim sSQL,rs,vRows, strtext
      Set rs = CreateObject("ADODB.Recordset")
      sSQL = "SELECT * FROM [" & sSheetName & "$]"

      rs.open sSQL, conn

      i=0
      do while not rs.eof
            for each field in rs.fields
                  if Trim(strtext)="" then
                        strtext=strtext & field.value
                  else
                        strtext=strtext & "      " & field.value
                  end if
            next
            i=i+1
            rs.moveNext()
      loop

      rs.close
      Set rs = nothing
      conn.close
      Set conn = nothing
      vRows=Split(strtext,vbTab)
      ReadDataFromExcel=vRows
End Function
0
Comment
Question by:YZlat
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
WMIF earned 500 total points
ID: 17869973
check out the answer in this question.  it does what you need.
http:Q_21369273.html
0
 
LVL 25

Expert Comment

by:kevp75
ID: 17871150
problem with working with excel files (as opposed to a spreadsheet saved as a csv or txt file) is that it can have multiple worksheets.

I think that code in the linnk WMIF posted is good if you know the worksheet nameas already (well...at least for the accepted answer)  I haven't tried out that FSO method to find the name, but that may be what you need.  Or even further down the post:

dim xlADOConn, rs, strFileName
          strFileName = server.mappath("/myfile.xls")
          set xlADOConn = Server.CreateObject("ADODB.Connection")
          Call xlADOConn.Open("Driver={Microsoft Excel Driver (*.xls)};" & _
            "DriverId=790;" & _
            "Dbq=" & strFileName & ";")
          Set rs = xlADOConn.OpenSchema(20)
          if not rs.eof then
            SheetName =  rs("Table_Name")
          end if
          set rs = nothing
          set xlADOConn = nothing






please no points for me...just pointing and elaborating a little on WMIF's post
0
 
LVL 22

Expert Comment

by:WMIF
ID: 17872279
whoa, that accepted answer is not what i remember reading earlier.  i must not have been paying that good of attention.  sorry about that.

@kevp75 - glad you found something in there to work for him though.
0
 
LVL 35

Author Comment

by:YZlat
ID: 17881737
Thanks! That was exactly what I needed
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

885 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