Author Topic: IScriptControl.ExecuteStatement Method  (Read 3762 times)

0 Members and 1 Guest are viewing this topic.

Offline José Roca

  • Administrator
  • Hero Member
  • *****
  • Posts: 2517
  • User-Rate: +209/-0
  • Gender: Male
IScriptControl.ExecuteStatement Method
« on: July 15, 2008, 12:45:48 AM »

The following code illustrates the use of the ExecuteStatement method.

Code: [Select]
' ########################################################################################
' 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
' ========================================================================================
« Last Edit: August 07, 2011, 08:02:24 AM by José Roca »