Error "Object variable or With block not set" in VBA

1

Searching, I have located how to download the files from an email to the folder I need, now I find the problem that is compressed in .zip and I have to unzip them to load them in the database.

Searching, I have seen this code, which gives me an error in the last step. They can help me, I do not know where the error is and looking for nothing. Thanks.

Sub Descomprimir()

Dim Destino As String
Dim Origen As String
Dim oApp As Object

Destino = "C:\Users\PTbarbera\Desktop\descagasDatos\pruebaZip"
Origen = "C:\Users\PTbarbera\Desktop\descagasDatos\prueba.zip"

 Set oApp = CreateObject("Shell.Application")

  oApp.NameSpace(Destino).CopyHere 
  oApp.NameSpace(Origen).Items

End Sub 

The last step is the one that gives me the error: oApp.NameSpace(Destino).CopyHere oApp.NameSpace(Origen).Items .

Do not know if I have to activate a reference?

    
asked by luis b 08.04.2018 в 20:50
source

1 answer

1

Here is another example of how to unzip a file. the macro decompresses the zip file into a fixed folder "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
        'No hacer nada
    Else
        'Carpeta de destino
        DefPath = "C:\test\"    ' Cambia a tu ruta / variable
        If Right(DefPath, 1) <> "\" Then
            DefPath = DefPath & "\"
        End If

        FileNameFolder = DefPath

        '        'Borre todos los archivos en la carpeta DefPath primero si lo desea
        '        En caso de error reanudar
        '        Kill DefPath & "*.*"
        '        En caso de error, vaya a 0 (GoTo 0)

        'Extraiga los archivos en la carpeta Destino
        Set oApp = CreateObject("Shell.Application")
        oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

        MsgBox "Aquí encuentras los archivos: " & FileNameFolder

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

Here are other useful examples (translate from English):

     
    
answered by 09.04.2018 / 14:52
source