Question

I am updating a VBA program (excel). At startup the program checks if it can find a directory which is on the office file server using:

FileSystemObject.FolderExists("\\servername\path")

If this is not found the program switches to offline mode and saves its output to the local hard disk (for later transfer), instead of directly to the fileserver.

This works OK, It's very quick if the computer can reach the path, however it can sometimes take a while (up to one minute) for the call to FolderExists to complete/time-out, especially if there is a network connection open but the required path does not exist (i.e. we are connected to some other LAN).

My Question(s):

  1. is there a quicker/better way to check for the existence of a network path using VBA?

  2. is there a way to have the user cancel the search done by FolderExists() when (s)he knows it cannot succeed because they're not in the office. I.e. is there some way to prematurely exit FolderExists() (or any other function call for that matter)

I want the solution to have as little user input as possible, which is why the check is done automatically, rather than just asking the user if (s)he's in the office or not in the first place.

Was it helpful?

Solution

If you're on a domain:

Check the LOGONSERVER environmental variable.

If there are two '\' symbols before the server name, it's connected to active directory and so you should do your check.

Otherwise, it isn't logged into the office network, so you can bypass the check.

If you aren't on a domain:

Probably your best bet is to run a ping against the target server.

If it can't get a ping response, it either isn't connected to the network, isn't connected to YOUR network, or the server is down. You don't want your code to run either way, in those cases.

MVPS.ORG and MSDN Forums both have some code samples for that,

OTHER TIPS

I use the Dir Command, targeting a shared folder on the server and trapping the error when not found.

Dir("\\Servername\aFolder\", vbDirectory)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top