Having problems with VBA. Run time error '91' appears to me. Any idea?

Viewed 84

I'm trying to unzip a file using VBA but get the run time error 91. This is my code:

Dim Destino As String
Dim Origen As String
Dim oAplica As Object

Destino = "C:\Users\Oscar Mayorga\Downloads"
Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
Set oAplica = CreateObject("Shell.Application")
oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items
1 Answers

Change Destino = "C:\Users\Oscar Mayorga\Downloads to Destino = "C:\Users\Oscar Mayorga\Downloads\". Also change declaration as String to Variant

Is this what you are trying?

Dim Destino As Variant
Dim Origen As Variant
Dim oAplica As Object

Destino = "C:\Users\Oscar Mayorga\Downloads\"
Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
Set oAplica = CreateObject("Shell.Application")
oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items

Explanation: You are using Late Binding. To understand this, use Early Binding as shown below. Set a reference to Microsoft Shell Controls and Automation from Tools | References

enter image description here

Option Explicit

Sub Sample()
    Dim Destino As Variant
    Dim Origen As Variant
    Dim oAplica As Shell32.Shell

    Destino = "C:\Users\Oscar Mayorga\Downloads\"
    Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
    Set oAplica = New Shell32.Shell
    oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items
End Sub

Now when you press spacebar after you type the bracket, you will notice the below

enter image description here

It expects a Variant. More about it can be read in Shell.NameSpace method

In case the above link dies

enter image description here

Related