使用vba检查网络连接

时间:2014-06-27 12:54:39

标签: vba ms-access

有没有办法检查vba中的NEtwork连接?

我正在使用此命令:

If Dir("O:\") = "" Then
    MsgBox "you have network connection"
Else
    MsgBox "No Connection"
End If

但它不起作用,我得到运行时错误

6 个答案:

答案 0 :(得分:4)

除了翻转if和else部分之外,你所做的几乎是正确的,

即。当Dir("O:\") = "" = 您没有连接时

当它返回某些东西意味着你有一个连接。

Dir函数用于返回指定目录中的第一个文件名和属性列表。

Sub Test_Connection()

 If (Len(Dir("O:\"))) Then
  MsgBox "Connected"
 Else
  MsgBox "No Connection"
 End If

End Sub

答案 1 :(得分:1)

我在Access 2007 VBA中测试了此链接的解决方案。

http://www.vbaexpress.com/forum/showthread.php?42466-Pinging-IP-addresses-in-Access-2007

它可以作为函数调用,可以在VBA代码中的任何位置使用,以通过名称或IP检测网络资源的可用性,并将结果重新布置为布尔值。

答案 2 :(得分:1)

我不知道你的问题是否已经解决。无论如何,我有一个使用Excel VBA的类似问题。 在我的网络上有一个磁盘站,我使用字母M将此站的共享文件夹映射为Windows中的网络文件夹。通常,在启动Windows后,当然磁盘站已启动并运行,网络驱动器显示在Windows资源管理器中,但它有一个红叉(未连接)而不是带有一些绿色(连接)的图标。仅在我在资源管理器中手动单击此网络位置后,它才变为绿色。我首先预计,当第一次发出像Dir这样的语句时,也可以通过我的Excel VBA程序建立连接(" M:\ abc")。但是没有结果,图标仍为红色。我总是需要先在资源管理器中手动点击。 最后,我在VBA中找到了一个解决方案,在Dir之前使用了一个假的" shellexecute ......探索M:...",使网络驱动器自动连接。

Declare Function ShellExecute Lib "shell32.dll" Alias _
  "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation _
  As String, ByVal lpFile As String, ByVal lpParameters _
  As String, ByVal lpDirectory As String, ByVal nShowCmd _
  As Long) As Long
'...
Dim RetVal As Long
Dim hwnd As Long
RetVal = ShellExecute (hwnd, "explore", "M:", vbNullString, vbNullString, SW_SHOWNOACTIVATE)
'...
a = Dir("M:\abc")

答案 3 :(得分:1)

我知道这是一个非常老的问题,我的回答不是很干净(因为它使用了标签),但是我发现它确实非常简单和可靠。

我只是不想让DLL文件运行超过52个运行时错误,所以我使用了“ on error goto”和一个标签。

这样,如果文件夹不可用,您不会收到错误消息(这对我来说是不可接受的,因为我需要其他人舒适地使用宏),代码只是落入IF语句中,我认为如果len函数返回0,它将消失。

On Error GoTo len_didnt_work 'this on error handler allow you to get past the 52 runtime error and treat the situation the way you want, I decided to go with a msgbox and to stop the whole sub

If Len(Dir("O:\Test\**", vbDirectory)) = 0 Then 'this is the test others have proposed, which works great as long as the folder _is_ available. If it is not I'd always just get the 52 runtime error
len_didnt_work: 'this is the label I decided to use to move forward in the code without the runtime 52 error, but it is placed inside the IF statement, it just aids it to work 'properly' (as I'd expect it to)
    MsgBox "Sorry, your folder is not available",vbcritical 'msgbox to notify the user
    On Error GoTo 0 'reset error handling
    Exit Sub 'end sub, since I wanted to use files from my "O:\Test\" folder
End If 
On Error GoTo 0`'reset error handling in case the folder _was_ available

答案 4 :(得分:1)

这类似于St3ve的回答,但以函数形式返回,如果已连接驱动器,则返回True,否则返回False

Function TestNetwork() As Boolean
On Error Resume Next 'ignore errors

If Len(Dir("O:\", vbDirectory)) = 0 Then
    TestNetwork = False
Else
    TestNetwork = True
End If

On Error GoTo 0 'reset error handling
End Function

我发现Len(Dir(“ O:\”))方法适用于外部驱动器,例如闪存盘,但不适用于映射的网络驱动器。该函数可以通过On Error Resume Next解决此问题,因此,如果O:\是已断开连接的映射驱动器,则系统将隐藏错误52并转到TestNetwork = False行。

像这样在您的代码中调用函数:

If TestNetwork() = True Then
    'Code if connected
Else
    'Code if not connected
End If

您可以通过命名函数Function TestNetwork(DirAddress As String) As Boolean并将此“ O:\”替换为DirAddress来泛化此代码以测试不同的目录。然后在调用时使用TestNetwork("O:\")或带引号的其他任何目录地址。

答案 5 :(得分:0)

我想再谈谈这个问题。
我使用 Scripting.FileSystemObject!
将检查文件夹是否存在并且不需要错误处理。
它适用于网络驱动器映射和网络文件夹。
只是不会检测网络计算机是否是他们的,例如:"\server"
但是 "\server\folder" 或 "Y:" 正在工作。

Dim Serverfolder As String
Serverfolder = "O:\"
Dim fdObj As Object
Set fdObj = CreateObject("Scripting.FileSystemObject")
If fdObj.FolderExists(Serverfolder) = False Then '= False is not needed!
'folder do not exists
else
'Folder exists
End if
相关问题