Solved

Read from Excel using VBScript

Posted on 2006-11-03
4
20,249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
WMIF earned 125 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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