求助:VBA解压Zip后将输出重命名为压缩文件名,现生成空文件
问题解决:解压Zip并重命名内部XLS文件
7-Zip命令修复方案
你当前的命令生成空文件,大概率是7-Zip路径未加引号导致的(如果路径含空格,cmd无法正确识别执行文件),同时可以添加参数限定只提取XLS文件,避免意外。修正后的VBA Shell命令如下:
Shell "cmd /c """ & pathTo7zip & """ e """ & file & """ -so -i!*.xls > """ & tempFolder & Replace(Mid(file, InStrRev(file, "\") + 1), ".zip", ".xls") & """"
关键修正点:
- 给
pathTo7zip前后添加双引号(""" & pathTo7zip & """),解决路径含空格的解析问题 - 添加
-i!*.xls参数,强制只提取XLS文件,匹配你的需求
Windows原生PowerShell方案
如果不想依赖7-Zip,可使用Windows自带的PowerShell实现,无需额外安装工具:
Dim zipPath As String, tempDir As String, targetName As String zipPath = file ' 你的源Zip文件路径 tempDir = tempFolder ' 临时文件夹路径(确保末尾带反斜杠) targetName = Replace(Mid(zipPath, InStrRev(zipPath, "\") + 1), ".zip", ".xls") ' 调用PowerShell完成解压+重命名 Shell "powershell -Command ""Expand-Archive -Path '" & zipPath & "' -DestinationPath '" & tempDir & "' -Force; " & _ "Rename-Item -Path (Get-ChildItem -Path '" & tempDir & "' -Filter *.xls).FullName -NewName '" & targetName & "'"""
命令说明:
Expand-Archive:原生解压Zip文件到指定临时目录,-Force覆盖已存在的文件Get-ChildItem:查找临时目录内的XLS文件(确保唯一)Rename-Item:将找到的XLS文件重命名为Zip文件对应的名称
内容的提问来源于stack exchange,提问作者cody
相关产品推荐
相关产品推荐

