VBA复制到zip文件返回错误

VBA复制到zip文件返回错误,vba,zip,Vba,Zip,我已经编写了一个脚本,使用从www.rondebruin.nl改编的代码将文件夹中的所有文件添加到zip文件中,但我一直收到一个错误“Object variable或With block variable not set” Function ZipDir(FolderName As String, ZipName As String) As String 'Copied from: http://www.rondebruin.nl/win/s7/win001.htm Dim FileNameZi

我已经编写了一个脚本,使用从www.rondebruin.nl改编的代码将文件夹中的所有文件添加到zip文件中,但我一直收到一个错误“Object variable或With block variable not set”

Function ZipDir(FolderName As String, ZipName As String) As String
'Copied from: http://www.rondebruin.nl/win/s7/win001.htm
Dim FileNameZip ', FolderName
Dim strDate As String, DefPath As String
Dim oApp As Object

'Create empty Zip File
NewZip ZipName

Set oApp = CreateObject("Shell.Application")
'Copy the files to the compressed folder
oApp.Namespace(ZipName).CopyHere oApp.Namespace(FolderName).items '<<ERROR HERE

'Keep script waiting until Compressing is done
On Error Resume Next
Do Until oApp.Namespace(ZipName).items.Count = _
    oApp.Namespace(FolderName).items.Count
    Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0

MsgBox "You find the zipfile here: " & ZipName
End Function
函数ZipDir(FolderName作为字符串,ZipName作为字符串)作为字符串
'复制自:http://www.rondebruin.nl/win/s7/win001.htm
Dim FILENAME ZIP',FolderName
Dim strDate作为字符串,DefPath作为字符串
作为对象的Dim oApp
'创建空Zip文件
新邮政编码
设置oApp=CreateObject(“Shell.Application”)
'将文件复制到压缩文件夹

oApp.Namespace(ZipName).CopyHere oApp.Namespace(FolderName).items'使用
Shell.Application
对象时,应将所有路径和文件名作为变量传递,而不是字符串

如果你看看罗恩的密码,你会发现他就是这么做的

正如他在那一页上所说:

注意:不要在代码中将例如FileNameZip设置为字符串 例子。这必须是一个变体,如果您更改它,代码将不会更改 工作


是否从ZipName和FolderName获取有效路径?是。我甚至尝试使用即时窗口--
?zipname
?Foldername
,然后将这些值粘贴回代码中。。。当我粘贴实际值时,它工作,但当我使用ZipName或FolderName时,它不工作……那么它从哪里获得ZipName和FolderName呢?它们是在调用此函数的方法的即时窗口中,还是针对此函数本身?如果您对其中的每一个进行Debug.Print,它是否提供了您所期望的内容?ZipName和FolderName将被传递到函数中。做一个Debug.Print让我得到了我所期望的。这里有一个屏幕截图链接(请注意,实际代码比我上面发布的要混乱一些):非常感谢!正是我错的地方。