Question

In Excel 2010 VBA, I'm using the FileExists property of the FileSystemObject to check whether a file exists. On my computer it works fine. But on another Excel 2010 computer, it reports that the file is not there when in fact we see in Windows Explorer that the file is there. In both cases, the file being checked is on the local hard drive.

I'm checking for the file right after using Shell to unzip the file. I use DoEvents after the unzip. The file is being extracted to the same folder that the zip file is in.

Here's the code:

Dim oShell As Object, oZippedFile As Object, oUnzipTargetFolder As Object
Dim FSO As Object, oFile As Object, oFolder As Object
Dim strZipfileFullpath As Variant, strTargetFolderpath As Variant, strTargetFullpath As Variant 'NOT AS STRINGS!! (Shell error if strings)
Dim bFileCheckFsoFileExist As Boolean

Set oShell = CreateObject("shell.application")
Set FSO = CreateObject("scripting.filesystemobject")

strZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)

Set oFile = FSO.GetFile(strZipfileFullpath)
Set oFolder = oFile.ParentFolder
strTargetFolderpath = oFolder.Path & Application.PathSeparator
strTargetFullpath = strTargetFolderpath & oShell.Namespace(strZipfileFullpath).Items.Item(0).Name

oShell.Namespace(strTargetFolderpath).CopyHere oShell.Namespace(strZipfileFullpath).Items.Item(0) 'this zip has only one file.
DoEvents

bFileCheckFsoFileExist = FSO.FileExists(strTargetFullpath)

Any ideas why this works fine on one computer, but on another computer reports that the file is not there even though it we see that it clearly is?

UPDATE:

I thought I'd add this note in case it may be helpful for others running into the same snag.

The basic underlying issue was that when using Shell to extract a zipped file, the Name property of the zipped file object does not include the extension If Windows Explorer is hiding extensions and the file has a registered extension. For example:

Dim strGofnZipfileFullpath As Variant
Dim oShell As Object, oShellZipfile As Object, oShellZippedFileInZipfile As Object
Dim strShellZippedFileInZipfileFilename As Variant 'NOT AS STRINGS!! (Shell error if strings)

strGofnZipfileFullpath = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", MultiSelect:=False)

Set oShell = CreateObject("shell.application")
Set oShellZipfile = oShell.Namespace(strGofnZipfileFullpath)
Set oShellZippedFileInZipfile = oShellZipfile.Items.Item(0) 'assumes only 1 file in zip. 
        strShellZippedFileInZipfileFilename = oShellZippedFileInZipfile.Name

If Windows Explorer is set to hide extensions and the zipped file has a registered extension, strShellZippedFileInZipfileFilename does not include the extension.

However, the zipped file object (oShellZippedFileInZipfile) also has a Path property which does include the extension. So you can get the filename including extension like this:

strShellZippedFileInZipfileFilename = Right(oShellZippedFileInZipfile.Path, InStr(StrReverse(oShellZippedFileInZipfile.Path), Application.PathSeparator) - 1)
Was it helpful?

Solution

One thing to keep in mind is the folder options within Windows explorer. This may not help you in this case, but I have had a number of fso interactions that have a huge issue with how files are displayed in Windows Explorer. Something as simple as your system is displaying the file extension, and the other is not, can sometimes be the culprit in looking for files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top