• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

IScriptControl.ExecuteStatement Method

Started by José Roca, July 15, 2008, 12:45:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following code illustrates the use of the ExecuteStatement method.


' ########################################################################################
' Microsoft Script Control example.
' 2008 José Roca - Use at your own risk.
' ########################################################################################

' SED_PBWIN  ' Use the PBWIN compiler
#COMPILE EXE
#DIM ALL
#INCLUDE "MSSCRIPT.INC"

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL pSc AS IScriptControl
   LOCAL strScript AS STRING
   LOCAL vRes AS VARIANT

   ' Create an instance of the Microsoft Script Control
   pSc = NEWCOM "MSScriptControl.ScriptControl"
   IF ISNOTHING(pSc) THEN
      MSGBOX "Error creating an instance of the Microsoft Script Control"
      EXIT FUNCTION
   END IF

   TRY
      ' Set the language. It can be "VBScript" or "JScript"
      pSc.Language = UCODE$("VBScript")
      ' Make an script
      strScript = "Function MyTest" & $CRLF & _
                  "  MyTest = ""Hello this is a test""" & $CRLF & _
                  "End Function"
      pSc.ExecuteStatement UCODE$(StrScript)
      ' Evaluates the result of the function "MyTest"
      vRes = pSc.Eval(UCODE$("MyTest"))
      MSGBOX VARIANT$(vRes)
      ' Test another script
      strScript = "FUNCTION GetCurrentDate" & $CRLF & _
                  "  GetCurrentDate = FormatDateTime(Date, 1)" & $CRLF & _
                  "END FUNCTION"
      pSc.ExecuteStatement UCODE$(StrScript)
      vRes = pSc.Eval(UCODE$("GetCurrentDate"))
      MSGBOX VARIANT$(vRes)
      ' And another one
      strScript = "FUNCTION ReportFolderStatus(fldr)" & $CRLF & _
                  "   DIM fso, msg" & $CRLF & _
                  "   SET fso = CreateObject(""Scripting.FileSystemObject"")" & $CRLF & _
                  "   IF (fso.FolderExists(fldr)) THEN" & $CRLF & _
                  "      msg = fldr & "" exists.""" & $CRLF & _
                  "   ELSE" & $CRLF & _
                  "      msg = fldr & "" does not exists.""" & $CRLF & _
                  "   END IF" & $CRLF & _
                  "   ReportFolderStatus = msg" & $CRLF & _
                  "END FUNCTION"
      pSc.ExecuteStatement UCODE$(StrScript)
      vRes = pSc.Eval(UCODE$("ReportFolderStatus(""C:\WINDOWS"")"))
      MSGBOX VARIANT$(vRes)
   CATCH
      MSGBOX MSScriptControl_GetErrorInfo(pSc, OBJRESULT)
   END TRY

   ' Releases the interface
   pSc = NOTHING

END FUNCTION
' ========================================================================================