【问题标题】:Call shell with spaces in file path在文件路径中使用空格调用 shell
【发布时间】:2014-02-02 02:30:11
【问题描述】:

下面的代码使用 winzip 解压缩一个 .zip 文件。但是,如果 FolderPath 包含空白,我会收到不可捕获的错误“无法打开文档。”,没有错误号。如果 FolderPath 包含空白,如何使用 Shell 解压缩此文件?谢谢,

 FolderPath = ThisWorkbook.Path & "\"

 Unzipfile = Dir(FolderPath & "*.zip")
 While UnzipFile <> ""
 if InStr(1, UnzipFile, ".zip") > 0 Then
 ShellStr = "C:\Program Files\WinZip\winzip32 e- " & FolderPath & UnzipFile & " " & FolderPath
 Call Shell(ShellStr, vbHide)

【问题讨论】:

    标签: shell vba zip unzip


    【解决方案1】:

    你需要用空格引用路径

    ShellStr = """C:\Program Files\WinZip\winzip32.exe"" e- """ & _
                FolderPath & UnzipFile & """ """ & FolderPath & """"
    

    【讨论】:

    • 蒂姆,感谢您的回复。不幸的是,我已经尝试了您的建议,虽然它成功调用了文件,但它会将它们解压缩到一个名为“New”的新文件夹中。文件名具有字符串“.../New Folder/”,因此间隔在“New”和“Folder”之间。您知道我如何确保它们在“新文件夹”中解压缩吗?谢谢。
    • 引用路径是这里的典型方法 - 我不知道有任何其他方法可以处理路径中的空格(除了不使用它们!)。不过请参阅我上面的编辑 - 忘记了目标文件夹路径中的前导引号。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多