vba - 建议通过命令行通过 vba 进行 7Z 解压

标签 vba cmd 7zip

我正在尝试通过 VBA 命令行解压缩 docx,是否可以提取 word 文件的内容,即提取到自定义路径时的文件夹结构。

如果我使用“7z e”命令,我可以提取到自定义路径,但文件夹结构会丢失。

如果我使用“7z x”命令,文件夹结构将保持不变,但提取是在“用户文件夹”下完成的

任何建议都会非常有帮助,

提前致谢。

代码:

Sub UnZip7Zip(strTargetPath As String, Fname As Variant, Fext As Variant)
Dim oApp As Object, FSOobj As Object, fol, objFiles As Object, lngFileCount As Long, Source As String, Destination As String
Dim MyFile As String, Outdir As String, Cmdstr As String
Dim FileNameFolder As Variant
If Right(strTargetPath, 1) <> Application.PathSeparator Then
    strTargetPath = strTargetPath & Application.PathSeparator
End If
FileNameFolder = strTargetPath & Fname
'create destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
    FSOobj.CreateFolder FileNameFolder
End If
Set objFiles = FSOobj.GetFolder(strTargetPath & Fname).Files
lngFileCount = objFiles.count
If (lngFileCount = 0) Then
    'Prompts whether to overwrite if files exist ' This extracts all files into a single folder
    Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """"
ElseIf (lngFileCount > 0) Then
    'Replace files in the destination 
    Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & "-aoa" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """" & "-aoa"
End If
Debug.Print Cmdstr
'MsgBox Cmdstr
'Extract to the specified path
Call Shell(Cmdstr, 1) ' Here i extract the zip file
'CommandLine Cmdstr, True
Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing
End Sub

最佳答案

在您的代码中:

  • 7-Zip 命令行版本为 7za.exe而不是7z.exe
  • Dim MyFile As String, Outdir As String声明声明 变量MyFileOutdir (并分配存储空间),但我 没有看到这些变量在您的代码(位置)中定义
  • ... & "-aoa" & ... 中缺少一个空格,应该是... & " -aoa" & ...

关于vba - 建议通过命令行通过 vba 进行 7Z 解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271901/

相关文章:

excel - 创建自安装宏?

excel - 如果不存在,如何创建具有特定值的属性 - VBA

cmd - 批处理脚本参数中的空格

xml - 如何批量解析xml文件(CMD)

windows - 在Powershell中通过7zip命令提取.zip时如何处理CRC错误?

email - Powershell-从循环中获取输出文件,使用7zip,电子邮件存档,然后终止原始输出

xml - 返回 XML 文件中的所有节点,即使它们为空

excel - 获取从今天开始的月份和年份

batch-file - MS-Win-CMD PushD/PopD 如何工作以及为什么不在 "For/f"-Loop 中

java - 在java中获取7-Zip信息