使用VBA解压缩文件而不提示我一次(对于任何对话框选择“Yes to All”)

时间:2017-05-25 23:05:20

标签: vba excel-vba excel

有一个解压缩代码我想调整我的需求。

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

在某处:

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

出现一个对话框,询问我是否要覆盖具有相同名称的文件 - 是的我确实要覆盖它们,但是没有回答对话框 - 我想将其硬编码到代码中。

enter image description here

我找到了这个页面https://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85).aspx,但我不知道如何添加这个参数#16,对于显示的任何对话框,“响应”是“全部”。 你可以帮助我吗?

最后一件事: 你可以为我解释oApp.Namespace(Fname).items行。 我自己真的想过这个猜测,但我想说明这一点。

1 个答案:

答案 0 :(得分:0)

导致没有问题或没有任何提示的代码如下:

 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

当然this site给了我很多帮助 - 它的CpyHere解释网站。

我不明白的一件事是FnameFileNumberFolder需要被声明为变体的原因。在我看来,它们应该被声明为String。看看这个截图。

enter image description here

但是当我以这种方式声明它们时,代码会给我错误。

enter image description here

看看这里,变量已经有了它们的值(第一张图片)。 FileNameVariableDefinePath变量具有完全相同的值,它看起来像一个字符串4 me。这有什么必要,我需要声明另一个变量 - FileNameVariable在这种情况下(在第17行)具有相同的值,但变体类型。 请向我解释一下。