Connect to Excel using VBscript

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Chad
Posts: 5
Joined: Fri Oct 29, 2010 1:06 pm

Connect to Excel using VBscript

Post by Chad » Mon Nov 15, 2010 12:28 pm

I am trying to write a vbscript (GetExcelData.vbs) to extract data from an excel file. I have been able to have LiveCode write the .vbs file and execute it but only if I already have the file path and file name in the procedure to write the *.vbs file. What I would like to do is to have the user select the file in which the data resides, then process the file and finally pass back the information into a datagrid. Does anybody know how I can achieve this.
Here is my function. It will get the data if the file is already open. I want the user to select the file, then the VBScript extracts the data from the file that has been selected by the user, then shuts it all down and deletes the VBScript.

Function GetExcelData pRangeRef
put "Dim ObjXL,tNumRows,tNumCols,tRetVal,tRow,tCol" & cr & \
"Set ObjXL = GetObject(," & ("Excel.Application") & ")" & cr & \
"tNumRows = ObjXL.Range(" & (pRangeRef) & ").Rows.Count" & cr & \
"tNumCols = ObjXL.Range(" & (pRangeRef) & ").Columns.Count" into tScript

put tScript & cr & "For tRow = 1 To tNumRows" & cr & \
"For tCol = 1 to tNumCols" & cr & \
"If tCol <> tNumCols Then" & cr & \
"tRetVal = tRetVal & ObjXL.Range(" & (pRangeRef) & ").Cells(tRow,tCol).Value & vbTab" & cr & \
"Else" & cr & \
"tRetVal = tRetVal & ObjXL.Range(" & (pRangeRef) & ").Cells(tRow,tCol).Value & vbCrLf" & cr & \
"End If" & cr & "Next" & cr & "Next" into tScript

put tScript & cr & "tRetVal = Left(tRetVal,Len(tRetVal) - 2)" & cr & \
"WScript.Echo tRetVal" into tScript

put "C:\VBSTemp.vbs" into tFile
put tScript into url("file:" & tFile)
set the hideConsoleWindows to true
get shell("cscript.exe //nologo" && tFile)
send "delete file" && q(tFile) to me in 1 second
if char -1 of it is CR then delete char -1 of it -- strip any trailing CR
return it
break
end switch
end GetExcelData

Any Help would be much appreciated

Post Reply