- Aug
- 2,058
- 83
While not documented very well in the help file, tcommand can be used in a VBScript (or JScript) to access data from a COM Server, such as Excel.
This example shows the VBScript code that I used, and then called, via the SCRIPT command;
Sample run;
Posting this mainly for my future reference, but others might also find it useful.
Joe
This example shows the VBScript code that I used, and then called, via the SCRIPT command;
Code:
@setlocal
@echo off
::Create the VBScript file
type <<- endtext > %_batchname:excel.vbs
TakeCommand.tcommand("echo Creating Object for Excel.Application")
Dim xlApp: Set xlApp = CreateObject("Excel.Application")
TakeCommand.tcommand("echo Don't show Excel.Application")
xlApp.Visible = False
TakeCommand.tcommand("echo Open workbook")
Dim wb: Set wb = xlApp.Workbooks.Open("E:\Documents\MLHealth_Corona.xlsx")
TakeCommand.tcommand("echo Open worksheet")
Dim ws: Set ws = wb.Worksheets("Sheet1")
'Assign values from cells to VBScript variables
TakeCommand.tcommand("echo Get data from the spreadsheet")
total = ws.Cells(2,7).Value
total_diff = ws.Cells(2,8).Value
recovered = ws.Cells(2,9).Value
recovered_diff = ws.Cells(2,10).Value
'Cleanup
TakeCommand.tcommand("echo Close the workbook")
wb.Close()
TakeCommand.tcommand("echo Quit the Excel.Application")
xlApp.Quit
set ws = Nothing
Set wb = Nothing
Set xlApp = Nothing
'Set TCC Environment Variables from VBScript variables
TakeCommand.tcommand("set total=" + CStr(total))
TakeCommand.tcommand("set recovered=" + CStr(recovered))
TakeCommand.tcommand("set total_diff=" + CStr(total_diff))
TakeCommand.tcommand("set recovered_diff=" + CStr(recovered_diff))
'Display the results back to TCC
TakeCommand.tcommand("echo The next two lines displayed are from excel.vbs")
TakeCommand.tcommand("echo Total Diff Recovered Diff")
TakeCommand.tcommand("echo %total %total_diff %recovered %recovered_diff")
endtext
:: Run the VBScript file
script %_batchname:excel.vbs
echo.
echo The environment variables that we set from VBScript are now in the TCC environment
echo.
echo Total Diff Recovered Diff
echo %total %total_diff %recovered %recovered_diff
endlocal
Sample run;
Code:
e:\utils>excel.btm
Creating Object for Excel.Application
Don't show Excel.Application
Open workbook
Open worksheet
Get data from the spreadsheet
Close the workbook
Quit the Excel.Application
The next two lines displayed are from excel.vbs
Total Diff Recovered Diff
4368 114 3037 38
The environment variables that we set from VBScript are now in the TCC environment
Total Diff Recovered Diff
4368 114 3037 38
Joe
Code:
_x64: 1
_admin: 1
_elevated: 1
TCC 27.00.18 x64 Windows 10 [Version 10.0.18363.1256]