Is it possible to reuse an opened shell command from Excel VBA

cmosate

New Member
Joined
Jul 27, 2014
Messages
2
I am using a shell command to communicate with a device under test and would like to open this shell once, change directory to a specific one and then send different text strings to it at various times during my test program. The code below works perfectly fine but opens and closed the shell each time it is used. I think it woud be quicker if I could reuse it until done and then exit out of it.

Here are 2 examples of a couple different strings I would like to pass into the cmdShell function at various times. I would like to send the first string, perform some measurements and then repeat with the 2nd string.

i.e. shellStr1 = ("cd \.. & cd c:\ezspi & testdm900 --widn 0x1020 --widv 0x1") then once the shell is open and the directory has been set send shellStr = ("testdm900 --widn 0x1020 --widv 0x5")

Is this possible using WShell Script or do I need another method?

Public Sub cmdShell(shellStr As String)
Dim Test As Double
Dim response As VbMsgBoxResult
Dim cmdObject As Object
Dim ReferenceTime As Double, ElapsedTime As Double

Set objShell = CreateObject("WScript.Shell")

Test = Shell("cmd /c " & shellStr, vbHide)
' Test = Shell("cmd /k " & shellStrvbNormalFocus)

End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hello, i think you cannot use the same window cmd but you can use more commands in it, for example:
Code:
Call Shell("cmd.exe /k cd /d d:\ & chdir d:\1 & cd /d c:\", 1)
 
Upvote 0
Thanks for the response!

I agree, I have sent multiple commands in the same string but not in different strings and/or at different times. I think another method like Power Shell or something else is required.
 
Upvote 0
I am not sure, but maybe you should try find the cmd window and try to place commands in it. Try to search in google:
"Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long". Maybe this helps...
 
Upvote 0
Try this:
Code:
Option Explicit

Private Const ConsoleWindowClassName As String = "ConsoleWindowClass"
Private Const WM_CHAR As Long = &H102

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
    (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long


Sub Test()
    'Optional - open new command window
    Shell "cmd /k", vbNormalFocus
    Application.Wait DateAdd("s", 1, Now)
    
    Send_Command "dir" & vbCrLf
End Sub


Public Sub Send_Command(command As String)

    'Send command to an existing command window
    
    Dim hWnd As Long
    Dim i As Integer
    
    hWnd = FindWindow(ConsoleWindowClassName, "Command Prompt")
    If hWnd = 0 Then hWnd = FindWindow(ConsoleWindowClassName, "C:\Windows\system32\cmd.exe")
    If hWnd <> 0 Then
        For i = 1 To Len(command)
            PostMessage hWnd, WM_CHAR, Asc(Mid(command, i, 1)), 0&
        Next
    Else
        MsgBox "Command window not found"
    End If
        
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,030
Messages
6,128,408
Members
449,448
Latest member
Andrew Slatter

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