I don’t suppose you know offhand whether excel 97 vba displays the same error?
(I suppose I could investigate myself, but if you already know…)
Brooke
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Public Function gblnFileExists
Al,
Thanks for posting this.
One possible little tweak:
Add an “Exit For” statement immediately after “gblnFileExists = True” – that way as soon as there’s a positive ‘hit’, the loop gets exited and the function can finish up.
Probably would make little or no difference in this case, where .FoundFiles.Count is likely to be a small number, but would make a difference in a situation where the foundfiles count were larger.
Gary
Seems like a lot of trouble for a file exist function. Maybe I’m missing something. If so, please enlighten me.
What do you think of this as a FileExist function:
Public Function fFileExists(strFullName)
fFileExists = cbool(len(dir(strFullName)))
End Function
Could it be that not all of Office supports Dir() but all apps support application.filesearch?
Safe bet that if the app runs VB/VBA, then Dir is going to work.
.FileSearch can be used to search for different types of files, but can only be run from a VBA host application (actually, I should amend that – you can run it from Office apps, no idea if it’s available say in Visio or Mappoint or whatever).
If you can set a reference to Microsoft Scripting Runtime (which can be available within an Office 97 context, depending on whether certain other components are installed), then you can make use of the File Scripting Object functions, as in this example:
Private Function bCheckTemplateExists(sTemplatePath As String) As Boolean
bCheckTemplateExists = FSO.FileExists(sTemplatePath)
End Function
(assumes FileSystemObject has already been declared and an FSO object created, at module level)
Gary
Gary,
Visio is a VBA host. In fact, it was the first third party product to license VBA when VBA was first introduced. MapPoint is off my map, so I can’t speak to it.
You can also use the FileSystemObject with late binding with a reference to the Office Object library rather than the scripting runtime.
No, it only makes available a number of methods for working with files and folders.
The best coverage I’ve seen of this (25+pp.) can be found O’Reilly’s VB & VBA In A Nutshell.
With regard to your other post, I have to (with a tinge of wistfulness for a less responsible time) agree with you.
This must be why so many programmers are ‘contract’ employees!
Hi Al
Sorry I missed this one, and I wanted to add the following in regards to your comment listed below:
<<>>
I some times have the need to use the GetShortPathName API to get the 8.3 path without any spaces that say a shell command can understand. It goes like this:
Public Declare Function GetShortPathName Lib “kernel32” _
Alias “GetShortPathNameA” (ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long
Maybe by using this function to generate the name for the Dir function it will eliminate the problem.
I hope this helps.
Wassim
I hereby update my old solution to eliminate a bug: if you passed into the function a filename of the empty string, the function returned true (due to the Dir command interpreting the “” as nothing which means get the next file — in this case the first file in the current folder). Very bad. The FolderExists function had the same problem.
Public Function fFolderExists(strFullName As Variant) As Boolean
If Len(strFullName) 0 Then
fFolderExists = Len(Dir(strFullName, vbDirectory))
End If
End Function
Public Function fFileExists(strFullName As String) As Boolean
If Len(strFullName) 0 Then
fFileExists = Len(Dir(strFullName, vbNormal))
End If
End Function
Thanks to Al for inspiring me to take a closer look!
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications