Last active
October 5, 2018 17:15
-
-
Save benabrahamson/1ed576b799c856661d0d4150b993852a to your computer and use it in GitHub Desktop.
BenWordPrompt is a macro that allows you to execute commands even if cmd.exe is blocked on the system. Adapted from code by Aaron Bush.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' BenWordPrompt by Ben Abrahamson | |
' Adapted from code by Aaron Bush | |
' --- | |
' BenWordPrompt is a macro that allows you to execute commands even if cmd.exe is blocked on the system. | |
Option Explicit | |
Public Sub BenWordPrompt() | |
Const lngCancelled_c As Long = 0 | |
Dim strCmd As String | |
Dim welcome As String | |
welcome = "BenWordPrompt by Ben Abrahamson" + vbNewLine + "Adapted from code by Aaron Bush" + vbNewLine + vbNewLine + vbNewLine + "Enter command to execute:" | |
strCmd = VBA.InputBox(welcome, "BenWordPrompt", "") | |
strCmd = "title BenWordPrompt && " + strCmd + " && PAUSE" | |
If VBA.LenB(strCmd) = lngCancelled_c Then | |
Exit Sub | |
End If | |
CommandLine strCmd, True | |
End Sub | |
Public Function CommandLine(command As String, Optional ByVal keepAlive As _ | |
Boolean = False, Optional windowState As VbAppWinStyle = VbAppWinStyle.vbHide) _ | |
As Boolean | |
On Error GoTo Err_Hnd | |
Const lngMatch_c As Long = 0 | |
Const strCMD_c As String = "cmd.exe" | |
Const strComSpec_c As String = "COMSPEC" | |
Const strTerminate_c As String = " /c " | |
Const strKeepAlive_c As String = " /k " | |
Dim strCmdPath As String | |
Dim strCmdSwtch As String | |
If keepAlive Then | |
If windowState = vbHide Then | |
windowState = vbNormalFocus | |
End If | |
strCmdSwtch = strKeepAlive_c | |
Else | |
strCmdSwtch = strTerminate_c | |
End If | |
strCmdPath = VBA.Environ$(strComSpec_c) | |
If VBA.StrComp(VBA.Right$(strCmdPath, 7), strCMD_c, vbTextCompare) <> _ | |
lngMatch_c Then | |
strCmdSwtch = vbNullString | |
End If | |
VBA.Shell strCmdPath & strCmdSwtch & command, windowState | |
CommandLine = True | |
Exit Function | |
Err_Hnd: | |
CommandLine = False | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment