Goal
I have a dashboard in Excel, and when a button is clicked, does the following:
1.) Pulls in criteria from the sheet such as start & end date, sort order, filters, etc. 2.) Create a long text string using those criteria. The string itself is python code that queries data from our Google Analytics account using the Google API, and saves it as a CSV on our drive. 3.) VBA should then open a shell of either the CMD window, or Python IDLE, both of which can execute python script. 4.) Script then runs in the background and saves the CSV 5.) Main excel dashboard uses Power Query to connect to the .CSV file that was created, and imports that data into a table to be displayed in my dashboard.
Progress
I have a totally working VBA code which creates the python script and does exactly what I need (exports our Google Analytics data to CSV and saves it to our drive). I then have my main Excel dashboard, which using Power Query, pulls in the data from that CSV export. All good up until that point. Now my issue is using VBA to run the python script through a shell.
Main issue
Since I work in Finance, I do not have Admin permissions to our network. Our anti-virus software (Sophos) thinks that when I open the CMD window shell and try to run the python script, that it's malicious activity and automatically terminates everything. I do not have this issue running other out-of-the-box CMD arguments, only when I try to run my python script. I have our IT team looking into what might be triggering this, but they're a bit weary about opening the flood gates just for me to be able to run this.
My workaround has been to try to open a shell of the standard the Python IDLE. With the below VBA, I've been able to successfully open the IDLE, open the python file that VBA created, and that's where I'm stuck.
In order to actually run the script in my python shell, I need to type CTRL+R+U. Here enlies the problem: When I use the SendKeys() command in VBA to try and send "CTRL+R+U" to the shell, it seems to run those commands
within my VBA window before the shell even finishes opening, so it doesn't actually send those keys to my shell, thus, the program doesn't run. If you're in the VBA window and click CTRL+R it will highlight the "Run" tab, which is not what I want.
I've tried adding a Wait() command to the VBA code after the shell execution command like this, but to no avail.
Option Explicit Sub RunScript() Dim objShell As Object Dim PythonExe, PythonScript As String Set objShell = CreateObject("Wscript.Shell") PythonExe = """C:\Users\MyName\AppData\Local\Programs\Python\Python38-32\Lib\idlelib\idle.pyw""""" PythonScript = "C:\Users\MyName\AppData\Local\Temp\Temp1_google-api-python-client-master.zip\google-api-python-client-master\My Code\test.py" objShell.Run PythonExe & PythonScript Application.Wait (100) With objShell objShell.SendKeys "%{R}" objShell.SendKeys "{U}" End With End Sub
I'm not sure if my issue is that Windows does not like sending keystrokes to non-native windows applications like my Python IDLE.
Happy to provide more info if needed.