7

Im having a problem with my excel vba macro. I need it to execute a batch file which is in the same folder as the excel workbook. The code works well sometimes. I don't know whats causing the error. Here's the code:

Sub writebatch()
  Sheets("code").Select
  Application.DisplayAlerts = False
  ActiveWorkbook.SaveAs FileName:=ThisWorkbook.path & "\code.bat",
  FileFormat:=xlTextPrinter, CreateBackup:=False
  Application.DisplayAlerts = True
  ThisWorkbook.Saved = True
  Shell "cmd.exe /k cd " & ThisWorkbook.path & "&&code.bat"
  Application.Quit
End Sub

It writes the batch file, but then doesn't execute it. Only once I got the command window to not close and it said the code.bat file could not be found. So the changedir command worked. Is it possible to run cmd.exe and run the code.bat with the relative path without having to changedir?

2
  • 2
    not sure I understand. You have an Excel workbook which you are saving as a batch file and then your trying to run the "Excel" file as a .bat? Commented Nov 23, 2012 at 13:48
  • Is the string "&&" in "&&code.bat" supposed to mean anything special, or is that really part of the file name? Commented Nov 23, 2012 at 13:53

6 Answers 6

8

First of all, when you launch a CMD instance you need to enclose the entire CMD argument if you use any type of operators (&):

CMD /K "command1 & command2"

And then enclose the sub-internal arguments, like this:

CMD /K "command "path with spaces" & command"

So you need to do something like this:

Shell "cmd.exe /k ""cd " & """ & ThisWorkbook.path & """ & " && code.bat"""

Notice I've used """ to escape a quote, but I don't know the way to escape a quote in VBA.

PS: remember to also enclose the code.bat if you have spaces, but ONLY if you have spaces.

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

1 Comment

It's either blackslash '\' or to use single quotes to contain the double quotes '"'
4

I'm pretty sure the problem is down to this line

Shell "cmd.exe /k cd " & ThisWorkbook.path & "&&code.bat"

You need a space in front of the && to separate it from the cd command and after it to separate it from the code.bat.

Shell "cmd.exe /k cd " & ThisWorkbook.path & " && code.bat"

Comments

3

Shell "cmd.exe /k cd /d" & ThisWorkbook.path & "&& code.bat"

here, without /d cmd will open in document folder. by /d it will open in d drive, you may change this as per your easy.

1 Comment

Hi, thanks for contributing to the site. As it stands your answer is a little short of our guidelines for good answers. Would you be able to expand it at all?
2

One way to insert a quote(") into a string is by using the character code conversion function Chr(34), 34 being the ASCII value for quotes(")

Comments

1

I hope it will useful for you.

Dim sfilename, fileName As String
Sub Run_file_bat()
    fileName = "" & ThisWorkbook.Path & "\code.bat" & ""

    VBA.Shell "Explorer.exe " & fileName, vbNormalFocus

End Sub

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

' Run Batch File

Sub RunBatchFile()

Dim batchFilePath As String
batchFilePath = "**actual path to your batch file**"

' Run the batch file and capture the process ID
Dim shellResult As Double
Dim shellProcess As Object
Set shellProcess = CreateObject("WScript.Shell")
shellResult = shellProcess.Run(batchFilePath, vbHide, True)

' Check if the batch file was executed successfully
If shellResult = 0 Then
    ' Batch file completed successfully
    MsgBox "Batch file completed successfully.", vbInformation
Else
    ' Batch file failed or returned an error code
    MsgBox "Batch file failed with error code: " & shellResult, vbExclamation
End If
End Sub

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.