使用VBA解压文件而不提示我一次(对任何对话框选择“全部是”)
有一个解压代码,我想调整4我的需要使用VBA解压文件而不提示我一次(对任何对话框选择“全部是”),vba,excel,Vba,Excel,有一个解压代码,我想调整4我的需要 Sub Unzip() Dim FSO As Object Dim oApp As Object Dim Fname As Variant Dim FileNameFolder As Variant Dim DefinePath As String ' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip"
Sub Unzip()
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefinePath As String
' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Fname = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip"
If Fname = False Then
'Do nothing
Else
'Destination folder
DefinePath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst\" ' Change to your path / variable
If Right(DefinePath, 1) <> "\" Then
DefinePath = DefinePath & "\"
End If
FileNameFolder = DefinePath
' 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
出现一个对话框,询问我是否要覆盖具有相同名称的文件-是的,我确实要覆盖它们,但不回答对话框-我想将其硬编码到代码中,请
我找到了这个页面,但我不知道如何添加这个参数#16,对于显示的任何对话框,它是“用“Yes to All”响应”
你能帮我吗
最后一件事:
您能为我解释一下oApp.Namespace(Fname).items
行吗。
我真的试着自己猜,但我想我要简短地说一下。没有问题或任何提示的代码如下所示:
Option Explicit
Sub Bossa_Unzip()
Dim FSO As Object
Dim oApp As Object ' oApp is the object which has the methods you're using in your code to unzip the zip file:
'you need to create that object before you can use it.
Dim Fname As Variant
Dim FileNameFolder As Variant ' previously Dim FileNameFolder As Variant
Dim DefinePath As String
' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Fname = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip"
If Fname = False Then
'Do nothing
Else
'Destination folder
DefinePath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst\" ' Change to your path / variable
If Right(DefinePath, 1) <> "\" Then
DefinePath = DefinePath & "\"
End If
FileNameFolder = DefinePath
' 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") ' you need to create oApp object before you can use it.
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items, 16
'MsgBox "You'll find the files here: " & DefinePath
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub
选项显式
子博萨乌解压()
作为对象的Dim FSO
Dim oApp As Object’oApp是一个对象,它具有您在代码中用于解压缩zip文件的方法:
'您需要先创建该对象,然后才能使用它。
Dim Fname作为变体
Dim FileNameFolder As Variant“以前Dim FileNameFolder As Variant
将路径定义为字符串
'Fname=Application.GetOpenFilename(文件筛选器:=“Zip文件(*.Zip),*.Zip”,MultiSelect:=False)
Fname=“C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip”
如果Fname=False,则
“什么也不做
其他的
'目标文件夹
DefinePath=“C:\Users\michal\SkyDrive\csv\bossa\mstcgl\u mst\”更改路径/变量
如果正确(定义路径,1)“\”则
定义路径=定义路径和“\”
如果结束
FileNameFolder=DefinePath
'如果需要,请先删除文件夹DefPath中的所有文件。
'出现错误时,请继续下一步
'Kill DefPath&'*.'
'在出现错误时转到0
'将文件解压缩到目标文件夹中
设置oApp=CreateObject(“Shell.Application”)“您需要先创建oApp对象,然后才能使用它。
名称空间(FileNameFolder).CopyHere-oApp.Namespace(Fname).items,16
'MsgBox“您可以在此处找到文件:”&DefinePath
出错时继续下一步
设置FSO=CreateObject(“scripting.filesystemobject”)
FSO.deletefolder环境(“Temp”)和“\Temporary Directory*”,True
如果结束
端接头
当然帮了我很多-它的CpyHere
解释网站
有一件事我不明白,为什么Fname
和FileNumberFolder
需要声明为variant。在我看来,它们应该声明为字符串。看看这个截图
但是当我以这种方式声明它们时,代码给了我错误
看看这里,当变量已经有了它们的值时(第一张图片)。filenamevaluate
和DefinePath
变量的值完全相同,看起来像一个字符串4me。这有什么必要,我需要声明另一个变量-filenamevaluate
,在这种情况下(在第17行中),具有相同的值,但类型不同。
请向我解释一下。
oApp.Namespace(Fname)
返回对zip文件(在本例中与文件夹同义)及其内容的引用<代码>项表示zip文件的内容。在这段代码中我不明白的是哪一行准确地解压了文件??以及为???oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items创建的oApp
变量是什么?
将所有项目从zip文件复制到FileNameFolder
。请注意,CopyHere
方法有一些选项,您可以尝试:“16”看起来像您想要的值,因此请尝试oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items,16
…和oApp
是一个对象,它具有您在代码中用于解压缩zip文件的方法:您需要先创建该对象,然后才能使用它。很抱歉,我错过了您文章底部的部分,您发布了我添加到MS文档中的相同链接…嗯,根据您的IT经验,解释可能会有所不同。尝试使用这两个-A)-B)-它是一个变量,因为该方法需要的是一个变量,而不是字符串。在OOP中,使用不同参数的相同方法称为重载。VBA不支持它。
Option Explicit
Sub Bossa_Unzip()
Dim FSO As Object
Dim oApp As Object ' oApp is the object which has the methods you're using in your code to unzip the zip file:
'you need to create that object before you can use it.
Dim Fname As Variant
Dim FileNameFolder As Variant ' previously Dim FileNameFolder As Variant
Dim DefinePath As String
' Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)
Fname = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl.zip"
If Fname = False Then
'Do nothing
Else
'Destination folder
DefinePath = "C:\Users\michal\SkyDrive\csv\bossa\mstcgl_mst\" ' Change to your path / variable
If Right(DefinePath, 1) <> "\" Then
DefinePath = DefinePath & "\"
End If
FileNameFolder = DefinePath
' 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") ' you need to create oApp object before you can use it.
oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items, 16
'MsgBox "You'll find the files here: " & DefinePath
On Error Resume Next
Set FSO = CreateObject("scripting.filesystemobject")
FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub