我正在尝试通过 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
声明声明 变量MyFile
和Outdir
(并分配存储空间),但我 没有看到这些变量在您的代码(位置)中定义... & "-aoa" & ...
中缺少一个空格,应该是... & " -aoa" & ...
关于vba - 建议通过命令行通过 vba 进行 7Z 解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271901/