Results 1 to 6 of 6
Thread: VBA to run shell commands
-
2020-09-21, 11:45 AM (ISO 8601)
- Join Date
- Jan 2009
VBA to run shell commands
I'm trying to use Visual Basic to run a Python script via shell commands.
If I open the DOS Command Prompt, this line of code does what I want (filepath is redacted a bit, but does contain spaces like shown)
Code:python "O:\Accountability\zzz projects\2020\This Project\testPython.py"
From looking online, I think this VBA should work, but it isn't.
Code:Option Explicit Sub runpython() Dim Ret_Val Dim args As String args = "'O:\Accountability\zzz projects\2020\This Project\testPython.py'" Ret_Val = Shell("python " + args, vbNormalFocus) 'or vbHide If Ret_Val = 0 Then MsgBox "Couldn't run python script!", vbOKOnly End If End Sub
So I think the syntax for a Shell command must differ somehow from the syntax when using the actual DOS Command Prompt.
Any help? Maybe something with spacing or single/double quotes?Last edited by JeenLeen; 2020-09-21 at 12:23 PM.
-
2020-09-21, 12:07 PM (ISO 8601)
- Join Date
- Mar 2007
- Location
- Oregon, USA
Re: VBA to run shell commands
FeytouchedBanana eldritch disciple avatar by...me!
The Index of the Giant's Comments VI―Making Dogma from Zapped Bananas
-
2020-09-21, 12:13 PM (ISO 8601)
- Join Date
- Nov 2006
- Location
- England. Ish.
- Gender
Re: VBA to run shell commands
Several suggestions:
Try putting the full path name in:
for example: Shell ("C:\Windows\System32\cmd.exe")
Also check to see if your anti-virus software is blocking it (possibly not if you can see the command box appearing)>
As to quotes, use double quotes around filenames with spaces, not single quotes. Confusingly you will have to double them up as well as they are inside a string.
Also use the concatenate operator "&" rather than "+":
args = """O:\Accountability\zzz projects\2020\This Project"""
Ret_Val = Shell("python " & args, vbNormalFocus) 'or vbHideLast edited by Manga Shoggoth; 2020-09-21 at 12:14 PM.
Warning: This posting may contain wit, wisdom, pathos, irony, satire, sarcasm and puns. And traces of nut.
"The main skill of a good ruler seems to be not preventing the conflagrations but rather keeping them contained enough they rate more as campfires." Rogar Demonblud
"Hold on just a d*** second. UK has spam callers that try to get you to buy conservatories?!? Even y'alls spammers are higher class than ours!" Peelee
-
2020-09-21, 12:23 PM (ISO 8601)
- Join Date
- Jan 2009
Re: VBA to run shell commands
Omission from redacting.
Assume both are O:\Accountability\zzz projects\2020\This Project\testPython.py
I've fixed the opening post.
That worked!
Thank you!
Also, I realize it should be if Ret_Val NE 0 (or however VBA does 'not equal').Last edited by JeenLeen; 2020-09-21 at 12:27 PM.
-
2020-09-21, 01:29 PM (ISO 8601)
- Join Date
- Nov 2006
- Location
- England. Ish.
- Gender
Re: VBA to run shell commands
Warning: This posting may contain wit, wisdom, pathos, irony, satire, sarcasm and puns. And traces of nut.
"The main skill of a good ruler seems to be not preventing the conflagrations but rather keeping them contained enough they rate more as campfires." Rogar Demonblud
"Hold on just a d*** second. UK has spam callers that try to get you to buy conservatories?!? Even y'alls spammers are higher class than ours!" Peelee
-
2020-09-21, 01:51 PM (ISO 8601)
- Join Date
- Jan 2009
Re: VBA to run shell commands
...and I just realized that my question wasn't exactly the right question
I think I'm actually needing to run this in VBScript. Or whatever a .vbs file is.
The real context is that I'm having SAS run the script via a %sysexec command on CScript. I thought VBA and VBScript were the same, but I'm seeing the differences as I look at guides on translating from one language to another.
Any recommendation on translating this to VBScript?
I've tried a few things based on guides, but all do nothing when I call it from SAS and give one of a handful of errors if I open the .vbs script.
OR if you know the VBScript to run a VBA subroutine, that could work... and I find it kinda funny as it adds another layer of convulution to my process. Currently it's SAS telling Windows to run VBA/VBScript to get it run Python. Trying to workaround some technology limitations that make running Python straight from SAS difficult.
---------------------------------edit: got solution----------------------------------
from https://stackoverflow.com/questions/...mmand-from-vbs
Code:Set oShell = CreateObject ("WScript.Shell") oShell.run "python ""O:\my file path\testPython.py""" Set oShell = Nothing
Last edited by JeenLeen; 2020-09-21 at 03:09 PM.