Excel VBA Shell命令在文件路径中带有空格 [英] Excel VBA Shell command with spaces in file path

查看:568
本文介绍了Excel VBA Shell命令在文件路径中带有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我正在尝试编写一个子例程,该子例程将在文件路径中运行带空格的shell命令。我已经对使用多个引号进行了很多研究,但是每当我运行代码时,仍然会出现文件未找到错误。输出到立即窗口的调试打印内容如下:

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"

将该字符串复制到Shell窗口中的效果很好,但是,从VBA中的Shell命令运行该字符串不起作用。我在做什么错?

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?

推荐答案

使用 Environ 函数获得特殊功能文件夹

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 & """"

这篇关于Excel VBA Shell命令在文件路径中带有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆