At my work place, different computers are in different subnets, and if the PCs are in the same subnet as the Samba server, I can reach the file server by going to \\myserv\MyFolder
, but for the PC is in a different subnet, the only way for me to reach the server is by using IP (i.e., \\1.2.3.4\MyFolder)
.
I am just wondering in VBA, if there is any way I can say:
if I can find the server using \\myserv
, then use myserv
, else use 1.2.3.4
?
Unfortunately I can not tweak the network at all and I want to set up this way so that at least when the IP changes, majority of my users could still use the tool by visiting \\myserv\MyFolder
.
Assuming you are on Windows environment, here's an alternative approach (without the actual code):
Kill( vsFileName )
This is an alternative if you don't want to mess around with win32 functions. It's a good idea to randomize the filename each time you run the code to avoid clashes.
Here is some code I use to check for server reachablility. It uses Windows winsock32 APIs and I haven't had any problems with it. I don't have to worry about privledges so I don't know how it'd handle that.
I've commented the code a fair bit so hopefully you'll understand whats going on should you need to tweak it. It with work as per the example sub I've used. I'll let you do the code that sets the folder after the network path is checked ;)
This isn't a trivial task but it's a tricky problem so I'm happy to share the code. Start with the functions you'll call to do the check - note how the paths are defined. I've tested these on my network and they all work, the Diskstation is checked by its network name and its IP:
Then API declarations (these go at the top of your module).
And then the generic network functions: