Excel VBA Shell command with spaces in file path

2019-07-26 12:50发布

Public Sub openEntryFiles()
Dim filePath, shellCommand, agingsEntry, invenEntry As String
filePath = Range("CustomerPath").Value2 & "\files\"
agingsEntry = "agings\entry_EP.bat"
invenEntry = "inven\entry_EP.bat"

shellCommand = """%ProgramFiles(x86)%\TextPad 5\TextPad.exe"" -r -q -u """ & filePath & agingsEntry & """ -u """ & filePath & invenEntry & """"
Debug.Print shellCommand
Shell shellCommand, 0
End Sub

I am trying to write a subroutine that will run a shell command with spaces in the file path. I have done lots of research about using multiple quotes, but I still get a file not found error whenever I run the code. The debug print that is outputted to the Immediate window reads:

"%ProgramFiles(x86)%\TextPad 5\TextPad.exe" -r -q -u "\\ablsgaat002\aclwin\Clients\*****\files\agings\entry_EP.bat" -u "\\ablsgaat002\aclwin\Clients\*****\files\inven\entry_EP.bat"

Copying that string into a shell window works great, however, running it from the Shell command in VBA doesn't work. What am I doing wrong?

1条回答
时光不老,我们不散
2楼-- · 2019-07-26 13:36

Use Environ function to get Special Folders

pathSpecial = Environ("ProgramFiles(x86)")
shellCommand = """" & pathSpecial  & "\TextPad 5\TextPad.exe"" -r -q -u """ & filePath & agingsEntry & """ -u """ & filePath & invenEntry & """"
查看更多
登录 后发表回答