在Excel 2010 VBA中,我正在使用FileSystemObject的FileExists属性,以检查文件是否存在。在我的电脑上,它工作正常。但是在另一个Excel 2010计算机上,它报告该文件在事实上,我们在Windows资源管理器中看到了该文件的内容。在这两种情况下,所检查的文件都在本地硬盘上。

我在使用shell以解压缩文件后立即检查文件。我在解压缩后使用DoEvents。该文件正在提取到zip文件所在的同一文件夹。

这是代码:

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)
.

任何想法如何在一台计算机上运行良好,但在另一台计算机上报告该文件的内容不存在,即使我们看到它清楚地是?

更新:

我以为我会添加这个说明,以防跑进同一障碍的人可能会有所帮助。

基本底层问题是,当使用shell提取zipped文件时,如果Windows资源管理器隐藏扩展名,则Zipped文件对象的Name属性不包括扩展名,并且该文件具有注册扩展名。例如:

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
.

如果将Windows资源管理器设置为隐藏扩展,并且zipped文件具有注册的扩展名,则strshellzippedfileinzipfilefilename不包含扩展名。

但是,压缩文件对象(OSHELLZIPPEDFILEINZIPFILE)也具有路径属性,该属性包含扩展。所以你可以获得包括这样的分机的文件名:

strShellZippedFileInZipfileFilename = Right(oShellZippedFileInZipfile.Path, InStr(StrReverse(oShellZippedFileInZipfile.Path), Application.PathSeparator) - 1)
.

有帮助吗?

解决方案

要记住的一件事是Windows资源管理器中的文件夹选项。在这种情况下,这可能无法帮助您,但我有许多FSO交互,其中包含文件在Windows资源管理器中的显示。像你的系统显示文件扩展一样简单的东西,另一个不是,有时可以是寻找文件的罪魁祸首。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top