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
Error: "Runtime Error 91" is a Visual BASIC error which means "Object variable not set". This indicates that the object was never created using the "Set" command before being used. Remedy: Be sure to use the SET statement to create the new oject.
To work around this problem, unprotect the worksheet to enable the macro to run. You can manually unprotect the worksheet or by using the Unprotect method in the macro.
First you must declare the object variable. Then you must assign a valid reference to the object variable using the Set statement. Similarly, a With... End With block must be initialized by executing the With statement entry point.
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
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
It expects a Variant
. More about it can be read in Shell.NameSpace method
In case the above link dies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With