Search code examples
vbazipunzip

Unzipping a file to a remote directory in VBA


I have a project where I need to extract a file from a zip, and then open that file. I had it working last week with the code:

 With CreateObject("Shell.Application")
        .Namespace("" & UnZippedFolder).copyhere .Namespace("" & ZipFolder & _ 
                       ZipFile).Items
 End With

When I tried to run it this week, I got a bunch of errors as I tried to debug it.

I've moved from 'simple' (above) to 'as explicit as I can figure out' (below). I'm currently getting the error, "Object variable or With block variable not set." with the line selected that has '*' at the end. I can't figure out why this error is being thrown, or how to fix it.

Dim WeekNum As Integer
Dim ZipFolder As String
Dim ZipFile As String
Dim UnZippedFile As String
Dim UnZippedFolder As String
Dim objShell
Dim UZipFold
Dim ZipFoldAndFile
If Proceed = False Then Exit Sub

WeekNum = Workbooks("personal.xlsb").Sheets("Dates").Range("WeekNum").Value
ZipFolder = "\\server\path\" ' obfuscated because I must, sorry
ZipFile = "Prefix" & "Week" & WeekNum & " (xlsx 07 format).zip" ' change the 11 to the last 2 digits of the year! 
UnZippedFolder = "\\server\path\" ' obfuscated, again, because I must
UnZippedFile = "Logging_11" & "Week" & WeekNum & " (xlsx 07 format).xlsx"

Set objShell = New Shell
UZipFold = objShell.Namespace("" & UnZippedFolder)
ZipFoldAndFile = objShell.Namespace("" & ZipFolder & ZipFile)

UZipFold.copyhere (objShell.Namespace("" & ZipFolder & ZipFile).Items) '*'

Solution

  • You must declare your paths or anything passed to the shell object as variants, not strings.

    See here: http://www.rondebruin.nl/windowsxpunzip.htm