建议使用 7Z 通过命令行解压缩 vba

Suggestion with 7Z unzip via command line via vba

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

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

如果我使用“7z x”命令,文件夹结构会保持不变,但提取是在 "User folder"

下完成的

任何建议都会很有帮助,

提前致谢。

代码:

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-Zipz.exe" & """" & " e " & """" & MyFile & """" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files-Zipz.exe" & """" & " x " & """" & MyFile & """"
ElseIf (lngFileCount > 0) Then
    'Replace files in the destination 
    Cmdstr = """" & "C:\Program Files-Zipz.exe" & """" & " e " & """" & MyFile & """" & "-aoa" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files-Zipz.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(并分配存储 space),但我 没有看到这些变量在您的代码中 已定义 (where)
  • ... & "-aoa" & ... 中缺少 space,应该是 ... & " -aoa" & ...