views:

800

answers:

1

I am updating a VBA program (excel). At startup the program checks if it can find a directory which is on the office fileserver 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.

+2  A: 

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,

Kevin Fairchild
The problem is that we don't use active directory. It's just a bunch of PCs and laptops and a samba server. 'echo %LOGONSERVER%' returns '\\<mycomputerName>'
Loopo
Ah. Ok, well, what about the ping solution? Pinging the server will take a second or two. Much easier to deal with than the potential wait time associated with FolderExists.
Kevin Fairchild
Just tested the ping solution, works like a dream and fast.. now why didn't I think of that? Thanks Kevin!
Loopo