The procedure for requesting AWR support has changed. Please read all about the new AWR product support process.
Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagevb
' Code Module
Sub Main
	Dim oShell, oExec As Object
	Dim DirPath As String
	Dim Dir_n_File As String
	Dim Exec_cmd As String
	Debug.Clear
	'
	DirPath = "C:\PythonDirectory\"                'Directory where python script exists
	Dir_n_File = DirPath & "msg_box.py"            'concatenate python script
	Exec_cmd = "py " & Dir_n_File                  'concatenate py with the scripts director and file name. py is a shortcut to Python.exe
	'
	Set oShell = CreateObject("WScript.Shell")     'set shell object
	Set oExec = oShell.Exec(Exec_cmd)              'execute shell object
	'
	Do While oExec.Status = 0                      'loop until python script is complete
    	Wait(1)
	Loop
	'
End Sub

...

Code Block
languagevb
' Code Module
Sub Main
	Dim oShell, oExec As Object
	Dim Return_str As String
	Dim arg As String
	Dim DirPath As String
	Dim Dir_n_File As String
	Dim Exec_cmd As String
	Dim arg1, arg2 As String
	Debug.Clear
	'
	a_real = 1                                            'Real part of a
	a_imag = 3                                            'Imaginary part of a
	b_real = 4                                            'Real part of b
	b_imag = 7                                            'Imaginary part of b
	operation = "add"                                     'add | subtract | multiply | divide
    '
	arg = Str(a_real) &  " " & Str(a_imag) &  " " & Str(b_real) &  " " & Str(b_imag) & " " & operation  'argument for the Python command line
	'
	DirPath = "C:\PythonDirectory\"                       'Directory where python script exists
	Dir_n_File = DirPath & "complex_math.py"              'concatenate python script
	Exec_cmd = "py " & Dir_n_File & " " & arg             'concatenate py with the scripts director and file name and argument. py is a shortcut to Python.exe
	'
	Set oShell = CreateObject("WScript.Shell")            'set shell object
	Set oExec = oShell.Exec(Exec_cmd)                     'execute shell object
	'
	Do While Not oExec.StdOut.AtEndOfStream               'read back print statement from python script
		Return_str = oExec.StdOut.ReadLine
		Debug.Print Return_str
	Loop
	'
	Do While oExec.Status = 0                              'loop until python script is complete
    	Wait(0.1)
	Loop
End Sub

...

A function main must be defined. Arguments are in the form of a list with index 0 being the title of the Python script and the next indices being the passed parameters. The results of the complex math are printed out and in the VBA script, the printed results are read in.

Example 3: Suppressing Command Window

Running Python scripts without pass parameters or the need to read Python script print statements, that is to only launch the Python script, an alternate shell method to the .Exec method is the .Run. The advantage of the .Run method is that the Windows command window can be suppressed. Here is an example of running the msg_box.py script with the command window supressed:

Code Block
languagevb
' Code Module
Sub Main
	Dim oShell As Object
	Dim DirPath As String
	Dim Dir_n_File As String
	Dim Run_cmd As String
	Debug.Clear
	'
	DirPath = "C:\PythonDirectory\"                'Directory where python script exists
	Dir_n_File = DirPath & "msg_box.py"            'concatenate python script
	Run_cmd = "py " & Dir_n_File                   'concatenate py with the scripts director and file name. py is a shortcut to Python.exe
	'
	Set oShell = CreateObject("WScript.Shell")     'set shell object
	oShell.Run(Run_cmd, 0, True)                   'run shell object. Parameter 0 is for command window suppression. True parameters tells the VBA
    '                                               script to wait for the Python script to complete
End Sub