VBA copy to zip file returns error
<p>I've written a script to add all files in a folder to a zip file using code adapted from www.rondebruin.nl, but I keep getting an error 'Object variable or With block variable not set'.</p>

<pre><code>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 '&lt;&lt;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: " &amp; ZipName
End Function
</code></pre>

<p>The code errors on the <code>oApp.Namespace(ZipName).CopyHere oApp.Namespace(FolderName).items</code> line. However if I explicitly declare the source and destination files like so: <code>oApp.Namespace("C:\MyZip.Zip").CopyHere "C:\Temp\MyFile.pdf"</code></p>

<p>If I change Either the zip name, or the file name from above, then it errors.</p>

<p>Any ideas?</p>