将文件解压缩到VBA中的远程目录

时间:2011-11-01 17:18:22

标签: vba zip unzip

我有一个项目需要从zip中提取文件,然后打开该文件。我上周用代码工作了:

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

当我本周试图运行它时,我尝试调试时出现了一堆错误。

我已经从'简单'(上图)变为'尽可能明确'(下图)。我目前收到的错误是“对象变量或未设置块变量”。选中的行最后有'*'。我无法弄清楚为什么会抛出这个错误,或者如何修复它。

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) '*'

1 个答案:

答案 0 :(得分:1)

您必须将路径或传递给shell对象的任何内容声明为变体,而不是字符串。

见这里:http://www.rondebruin.nl/windowsxpunzip.htm