Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Created September 15, 2016 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxperscientiam/bd836c49c5db3a644e5c72ad2d023b30 to your computer and use it in GitHub Desktop.
Save paxperscientiam/bd836c49c5db3a644e5c72ad2d023b30 to your computer and use it in GitHub Desktop.
execShell VBA module by Robert Knight
' This User Defined Function was written by Robert Knight of StackOverflow
' Designed for Excel 2011, though it might work in the Windoze version too.
' It is shared here for posterity.
Private Declare Function popen Lib "libc.dylib" (ByVal command As String, ByVal mode As String) As Long
Private Declare Function pclose Lib "libc.dylib" (ByVal file As Long) As Long
Private Declare Function fread Lib "libc.dylib" (ByVal outStr As String, ByVal size As Long, ByVal items As Long, ByVal stream As Long) As Long
Private Declare Function feof Lib "libc.dylib" (ByVal file As Long) As Long
Function execShell(command As String, Optional ByRef exitCode As Long) As String
Dim file As Long
file = popen(command, "r")
If file = 0 Then
Exit Function
End If
While feof(file) = 0
Dim chunk As String
Dim read As Long
chunk = Space(50)
read = fread(chunk, 1, Len(chunk) - 1, file)
If read > 0 Then
chunk = Left$(chunk, read)
execShell = execShell & chunk
End If
Wend
exitCode = pclose(file)
End Function
Sub RunTest()
Dim result As String
Dim exitCode As Long
result = execShell("echo Hello World", exitCode)
Debug.Print "Result: """ & result & """"
Debug.Print "Exit Code: " & str(exitCode)
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment