0

I'm trying to run a BAT file from a Macro in excel:

Sub RunBatchFile()
    Dim batchPath As String
    Dim cmdPath As String
    
    ' Set the path to your batch file
    batchPath = "C:\cmdb_si_parent.bat"
    
    ' Set the path to cmd.exe
    cmdPath = "C:\Windows\System32\cmd.exe"
    
    ' Run the batch file using the Shell function and cmd.exe with /k parameter
    Shell Chr(34) & cmdPath & Chr(34) & " /k " & Chr(34) & batchPath & Chr(34), vbNormalFocus
End Sub

I'm getting Access is denied. I don't know why its saying that because I can perfrectly execute the bat file from my file explorer.. Getting the same message when I replace the bat file to a different folder.

My bat file:

powershell.exe -executionpolicy remotesigned -File  "\\domain\runparentcheck.ps1"
Toon
  • 29
  • 8
  • Sounds like an issue related to [security settings](https://customer.precisely.com/s/article/When-running-a-batch-script-getting-Excel-error-Access-Denied-360024514992?language=en_US) – Foxfire And Burns And Burns Jun 22 '23 at 11:00
  • TBH, without knowing the content of your batch file we'd be guessing. My initial guess is that when invoking the batch file from 'explorer' it defines the working directory as that of the batch file. Your VBA however does not explicitly define the working directory. You could, in such a case, simply add `@CD /D "%~dp0."` as the new first line of your batch file, or [do it in VBA](https://stackoverflow.com/a/42789253). – Compo Jun 22 '23 at 11:21
  • @Compo please see my bat file. It executes a PowerShell script. (the bat file runs perfectly when I execute it from my file explorer) ps: I don't run the ps script from a macro in excel because my excel is crashing then. – Toon Jun 22 '23 at 12:16
  • What would be the reason for running a batch file? What is wrong with replacing `cmd.exe` with `WindowsPowerShell\v1.0\powershell.exe` and running the PowerShell parameters as its arguments? Also we have no idea if `\\domain` is connected and available, or even accessible by the end user. Additionally, as you've introduced another script, what's the contents of `runparentcheck.ps1`? and is the working directory defined within that? – Compo Jun 22 '23 at 12:51

0 Answers0