Christophe C.
Christophe C.

Reputation: 147

Pass in multiple arguments in VBA to run a Python script

Through VBA, I would like to execute a shell command that launches a Python script.

Private Type STARTUPINFO
  cb As Long
  lpReserved As String
  lpDesktop As String
  lpTitle As String
  dwX As Long
  dwY As Long
  dwXSize As Long
  dwYSize As Long
  dwXCountChars As Long
  dwYCountChars As Long
  dwFillAttribute As Long
  dwFlags As Long
  wShowWindow As Integer
  cbReserved2 As Integer
  lpReserved2 As Long
  hStdInput As Long
  hStdOutput As Long
  hStdError As Long
End Type

Private Type PROCESS_INFORMATION
  hProcess As Long
  hThread As Long
  dwProcessID As Long
  dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
  hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
  lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
  lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
  ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
  ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
  lpStartupInfo As STARTUPINFO, lpProcessInformation As _
  PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal _
  hObject As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Public Sub ExecScript(cmdline As String)
  Dim proc As PROCESS_INFORMATION
  Dim start As STARTUPINFO
  Dim ReturnValue As Integer

  'Initialize the STARTUPINFO structure:
  start.cb = Len(start)

  'Start the shelled application:
  ReturnValue = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

  'Wait for the shelled application to finish:
  Do
    ReturnValue = WaitForSingleObject(proc.hProcess, 0)
    DoEvents
  Loop Until ReturnValue <> 258

  ReturnValue = CloseHandle(proc.hProcess)

End Sub 

Public Function DataFormatting(inputData As Variant) As String
  Dim dataRank As Integer

  DataFormatting = "["
  For dataRank = 0 To UBound(inputData)
    inputData(dataRank) = Replace(inputData(dataRank), " ", "")
    If dataRank = 0 Then
      DataFormatting = DataFormatting & "\`" & """" & inputData(dataRank) & "\`" & """"
    Else
      DataFormatting = DataFormatting & ",\`" & """" & inputData(dataRank) & "\`" & """"
    End If
  Next dataRank
  DataFormatting = DataFormatting & "]" & """"
End Function

Public Sub RunPython()
  Dim oCmd as String, scriptPath as String, scriptName as String, campaign as String, datas as String 

  scriptPath = ActiveWorkbook.Path & "\Scripts\"
  scriptName = "EngineExecution.py"
  campaign = "Nom de la campagne"
  planningPeriods = checkboxSelected
  datas = DataFormatting(planningPeriods)

  oCmd = "pythonw.exe " & scriptPath & scriptName & " " & """" & campaign & """" & " " & """" & datas
  Call ExecScript(oCmd)
End Sub

When I execute this command with PowerShell, it works but not with VBA.

python .\EngineExecution.py "Nom de la campagne" "[\`"12/10/2020-18/10/2020\`",\`"05/10/2020-11/10/2020\`"]"

Here is the start of my Python code. Perhaps this is where the error comes from?

import sys
import json

campaign = sys.argv[1]
ppSelected = json.loads(sys.argv[2])

Could you please help me to make it work properly with VBA ?

Thanks in advance

Upvotes: 0

Views: 1540

Answers (2)

Christophe C.
Christophe C.

Reputation: 147

Thank you all for your help. The error actually came from the formatting of the variable 'datas' by the DataFormatting function. Below is the code that works.

Public Function DataFormatting(inputData As Variant) As String
  Dim dataRank As Integer

  DataFormatting = "["
  For dataRank = 0 To UBound(inputData)
    If dataRank = 0 Then
      DataFormatting = DataFormatting & """""" & inputData(dataRank) & """"""
    Else
      DataFormatting = DataFormatting & "," & """""" & inputData(dataRank) & """"""
    End If
  Next dataRank
  DataFormatting = DataFormatting & "]"
End Function

Upvotes: 0

Parfait
Parfait

Reputation: 107652

Calling an external command from VBA is very straightforward and does not require your extensive need of creating a process. Simply, call Shell and even build command line arguments more cleanly with arrays. Below updates your RunPython subrountine, assumming all arguments are correctly specified:

Public Sub RunPython()
    Dim args(0 To 3) As String
    Dim pyCmd As String
    Dim i As Integer

    args(0) = "python"
    args(1) = ActiveWorkbook.Path & "\Scripts\EngineExecution.py"
    args(2) = "Nom de la campagne"
    args(3) = DataFormatting(checkboxSelected)

    pyCmd = args(0)
    For i = 1 To UBound(args)
        pyCmd = pyCmd & " """ & args(i) & """"
    Next i        
    Debug.Print pyCmd                                ' CHECK COMMAND LINE CALL

    'RUN PYTHON SCRIPT WITH ARGS
    Shell pyCmd, vbNormalFocus
End Sub

Upvotes: 1

Related Questions