gpt4 book ai didi

excel - 将包含文件的文件夹解压到所选位置

转载 作者:行者123 更新时间:2023-12-02 15:16:12 26 4
gpt4 key购买 nike

团队,我正在努力从 VBA 代码中提取 zip 文件,但出现错误,这是我的代码:

Sub Un_Zip_File()
Dim flname As String
Call PathCall
flname = Dir(impathn & "Transactions*.zip")
Call PathCall
Call UnZip_File(impathn, flname)
End Sub

Sub UnZip_File(strTargetPath As String, fname As Variant)
Dim oApp As Object, FSOobj As Object
Dim FileNameFolder As Variant

If Right(strTargetPath, 1) <> Application.PathSeparator Then
strTargetPath = strTargetPath & Application.PathSeparator
End If

FileNameFolder = strTargetPath

'destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
FSOobj.CreateFolder FileNameFolder
End If

Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname).Items

Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing

End Sub

当我运行 Un_zip_file 宏时,出现错误:

Object variables or with block variable not set

调试后继续

oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(fname).Items

最佳答案

这是另一个如何解压缩文件的示例。
宏将 zip 文件解压缩到固定文件夹“C:\test\”

Sub Unzip()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String

Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
MultiSelect:=False)
If Fname = False Then
'Do nothing
Else
'Destination folder
DefPath = "C:\test\" ' Change to your path / variable
If Right(DefPath, 1) <> "\" Then
DefPath = DefPath & "\"
End If

FileNameFolder = DefPath

' 'Delete all the files in the folder DefPath first if you want
' On Error Resume Next
' Kill DefPath & "*.*"
' On Error GoTo 0

'Extract the files into the Destination folder
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

MsgBox "You find the files here: " & FileNameFolder

On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub

关于excel - 将包含文件的文件夹解压到所选位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35717193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com