How can I define several parameters to be read by an file .exe?

Lccs

New Member
Joined
Apr 26, 2021
Messages
5
Office Version
  1. 2013
Platform
  1. Windows
I would like to add several parameters in Excel to be read by an .exe file (python script). I have these two main functions in VBA that I am using for this. Does anyone know how I can change them to allow the incorporation of multiple parameters? I'm only able to insert one parameter in the 'module_type'.
Thank you so much for your help!

VBA Code:
Public Sub Import_data(ByVal title As String, ByVal python_script As String, ByVal table_name As String, ByVal procedure_name As String, Optional update_log As Boolean = False, Optional module_type As String, Optional get_file As Boolean = True)
Dim mypath As String

MsgBox ("Starting to import " & title & " data")

'mypath = Application.GetOpenFilename(, , "Import executable file")
If get_file Then
    MyFile = Application.GetOpenFilename(, , "Import " & title & " file")
    If MyFile = "False" Then Exit Sub
End If
mypath = ThisWorkbook.Path

schemaName = get_schema()
If get_file Then
Call RunPythonScript(mypath, python_script, """" & module_type & """ """ & get_database() & """ """ & connection_string("python") & """ """ & schemaName & """ --file_path """ & MyFile & """ --table """ & table_name & """")
Else
Call RunPythonScript(mypath, python_script, """" & module_type & """ """ & get_database() & """ """ & connection_string("python") & """ """ & schemaName)
End If

Dim cnn As New ADODB.Connection
Dim ConnectionString As String
Dim str As String
Dim rst As New ADODB.Recordset

ConnectionString = connection_string()
cnn.Open ConnectionString


'Change myFile to include to exclude '

MyFile = Replace(MyFile, "'", "")

If procedure_name <> "" Then
    If update_log Then
        Call correrQuery("exec " & procedure_name & " '" & MyFile & "'")
    Else
        Call correrQuery("exec " & procedure_name)
    End If
End If

MsgBox ("Imported " & title & " data successfully.")

Code:
Public Sub RunPythonScript(ByVal exe_path As String, ByVal exe_name As String, Optional ByVal params As String = "")
    
fullCmd = exe_name

If params <> "" Then
    fullCmd = fullCmd & " " & params
End If

Debug.Print fullCmd
Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1

wsh.CurrentDirectory = exe_path
wsh.Run fullCmd, windowStyle, waitOnReturn
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
It looks like RunPythonScript already accepts multiple parameters, which the caller specifies as a string in the params argument.
 
Upvote 0
It looks like RunPythonScript already accepts multiple parameters, which the caller specifies as a string in the params argument.
Thanks for your answer! Sorry, I just saw this now...
I need to provide the Import_data function with a range of values (for example through a list). How can I adjust that to convert it into a string?
Another example, I tried to mannualy enter the values, but it gave an error:
Import_data "Run", "script.exe", "", "", True, ("Hello", 1), False --> A "sintax error" error appears.

I am sorry to bother you again, I am quite new with VBA.
Thank you!
 
Upvote 0
I need to provide the Import_data function with a range of values (for example through a list). How can I adjust that to convert it into a string?
You would add another argument to Import_data. This could an array or a string or another data type/structure. How you handle it within Import_data is up to you and determined by the syntax/format that the Python script is expecting for this multiple parameter argument, e.g. should it be comma-separated, surrounded by quotes, etc.

Here's one way, with Import_data2 accepting a new 5th argument, ByVal params As Variant, which is an array whose values are converted to a comma-separated string surrounded by double quotes. You would then pass the allParams string to the Python script.
VBA Code:
Public Sub Import_data2(ByVal title As String, ByVal python_script As String, ByVal table_name As String, ByVal procedure_name As String, ByVal params As Variant, Optional update_log As Boolean = False, Optional module_type As String, Optional get_file As Boolean = True)
    Dim i As Long
    Dim allParams As String
    allParams = ""
    For i = 0 To UBound(params)
        allParams = allParams & params(i) & ", "
    Next
    allParams = Chr(34) & Left(allParams, Len(allParams) - 2) & Chr(34)
    Debug.Print allParams
End Sub
A test routine:
VBA Code:
Public Sub Test()
    Import_data2 "Run", "script.exe", "", "", Array("Hello", 1, 99, Date), True, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,199
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top