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

时间:2015-02-02 05:59:53

标签: 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

1 个答案:

答案 0 :(得分:0)

在您的代码中:

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