0

I need to run a command (eventually a script) remotely on a linux server invoked locally from within a VBA macro on my windows machine. I tested the command outside of VBA from windows command prompt and it works, but I am not sure how to do the same from within the VBA macro since I need to pass argument values which are the selected values of combobox. I am also unable to redirect the output of Shell to some log file. I looked up few articles online and was able to come up with the following code:

Private Sub UserForm_Initialize()
    comboBox1.List = Array("A", "B", "C", "D")
    comboBox2.List = Array("1", "2", "3", "4")
End Sub


Private Sub CommandButton1_Click()
    Call runCommand
    Unload Me
End Sub


Sub runCommand()
    Dim Ret_Val
    Dim Arg1 As String
    Dim Arg2 As String
    Dim command As String

    Arg1 = comboBox1.Value
    Arg2 = comboBox2.value

    command = "C:\Program Files\PuTTY\plink.exe" & " -v" & " " & "user@host" & " -pw" & " " & "testpw" & " " & "echo &Arg1,&Arg2" 

    Ret_Val = Shell(command & ">C:\logs\log.txt", 1)
    If Ret_Val = 0 Then
        MsgBox "Error!", vbOKOnly
    End If
End Sub

Any idea what I am missing here please?

4
  • 1
    So the problem is, "it doesn't work". Are you getting any specific errors or anything more tangible? "here's my broken code, please fix it" isn't exactly how this site works... Commented Jan 10, 2019 at 21:56
  • 3
    You're passing the string "echo &Arg1,&Arg2" instead of substituting the values of Arg1 and Arg2 Commented Jan 10, 2019 at 21:58
  • 2
    You need something more like: command = "C:\Program Files\PuTTY\plink.exe -v user@host -pw testpw echo " & Arg1 & " " & Arg2 If that doesn't work, then provide an example of a known working command line of the type you're trying to generate. Commented Jan 10, 2019 at 22:07
  • @TimWilliams, many thanks, your solution worked for me! Commented Jan 11, 2019 at 18:06

1 Answer 1

1

You're passing the string "echo &Arg1,&Arg2" instead of substituting the values of Arg1 and Arg2

You need something more like:

command = "C:\Program Files\PuTTY\plink.exe -v user@host -pw testpw echo " & _
           Arg1 & " " & Arg2 

If that doesn't work, then provide an example of a known working command line of the type you're trying to generate.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.