2

I'm trying to execute a bat. file from vba but can't get it working (after viewing some of the threads from the subject).

here is the code now:

Dim filet As String
Dim numero As String

Const pekka = "TIEDOSTO"
Const sami = "NUMEROT"
filet = Range(pekka).Cells(1, 1).value
numero = Range(sami).Cells(1, 1).value

commandstring = "D:"
commandstring2 = "cd folder name"
commandstring3 = "Create-tri.bat" + " " + filet + " " + numero
Call Shell("cmd.exe /S" & commandstring & commandstring2 & commandstring3, vbNormalFocus)

So according to my logic, this code should first access D, then the wanted folder and then execute the bat. file with the given parameters (filet and numero). What am I doing wrong here?

Best regards and thanks in advance,

Johannes

0

1 Answer 1

0

Try to remove the Call, it is depreciated in VBA.

Then use Shell "cmd.exe /S" & commandstring & commandstring2 & commandstring3.

To see what you are actually trying to execute, use:

Debug.Print "cmd.exe /S" & commandstring & commandstring2 & commandstring3

What do you get? Most probably it is not an executable command, ending on .bat Some reference here - Execute .bat file from Excel VBA Macro

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

2 Comments

Thanks for the answer. Hmm, after editing the code according to your instructions the debug.print looks pretty much as the code which I would normally manually write to the terminal. The problem is that the shell command doesn't actually seem to print anything to the terminal itself. It just opens it.
@JohannesJuolahti - I am not sure what the /S does, probably you may try with /k as in the accepted answer.

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.