vba 检查 LAN 环境和取消检查

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/250214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:01:03  来源:igfitidea点击:

Checking for LAN environment and Cancel check

filevbanetworkingpath

提问by Loopo

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:

我正在更新 VBA 程序(excel)。在启动时,程序会检查是否可以使用以下命令找到办公室文件服务器上的目录:

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

这工作正常,如果计算机可以到达路径,它会非常快,但是有时可能需要一段时间(最多一分钟)才能完成对 FolderExists 的调用/超时,特别是如果有网络连接打开但所需的路径不存在(即我们连接到其他某个 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)

  1. 有没有更快/更好的方法来使用 VBA 检查网络路径是否存在?

  2. 有没有办法让用户取消 FolderExists() 完成的搜索,当他知道它不能成功时,因为他们不在办公室。即有什么方法可以过早退出 FolderExists() (或任何其他与此相关的函数调用)

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.

我希望解决方案的用户输入尽可能少,这就是为什么检查是自动完成的,而不是首先询问用户他(她)是否在办公室。

回答by Kevin Fairchild

If you're on a domain:

如果您在域中:

Check the LOGONSERVER environmental variable.

检查 LOGONSERVER 环境变量。

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.

可能你最好的选择是对目标服务器运行 ping。

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.

如果它无法获得 ping 响应,则它要么未连接到网络、未连接到您的网络,要么服务器已关闭。在这些情况下,您不希望代码以任何一种方式运行。

MVPS.ORGand MSDN Forumsboth have some code samples for that,

MVPS.ORGMSDN 论坛都有一些代码示例,

回答by Rob Gibson

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

我使用Dir命令,针对服务器上的共享文件夹并在未找到时捕获错误。

Dir("\Servername\aFolder\", vbDirectory)