检测 Outlook 是否通过公司局域网或 Wifi 连接到Microsoft Exchange



>我在Outlook中运行一个例程,该例程将检查"已发送"文件夹,并将查看公用文件夹以查看"已发送"文件夹中的电子邮件是否需要移动到公用文件夹。 这运作良好。问题是我希望仅当 Outlook 通过 Wifi 上的公司 LAN 连接到交换时,才执行此例程。 原因是当员工在家使用 Outlook 时,他们有时不希望执行该例程。所以问题是如何检测Outlook是否通过公司LAN或WIFI连接?我找到了一种方法来检测我是否通过使用此子和相关功能连接到我的公司网络。 为此,我尝试访问公司服务器上的文件。 这不是那么优雅,但它让我走得那么远

Sub TestNetwork()
Dim connected As Boolean
connected = CheckForNetwork()
If connected = False Then
    MsgBox ("not connected")
Else
    MsgBox ("connected")
End If
End Sub
Function CheckForNetwork() As Boolean
On Error GoTo end
If Len(Dir("\company_namecompany_namecompany_name-000Employee _     
          Employee Name*.*")) > 0 Then
    CheckForNetwork = True
End If
Exit Function
end:
End Function

这会告诉我我是否连接到我们公司的服务器,但它不会告诉我它是否通过我的家庭网络、机场的公共 WiFi 点或其他任何地方连接。我需要了解 Outlook 是否通过公司网络或任何其他连接进行连接。

我将非常感谢有关此事的任何帮助

感谢Sorceri的建议,我找到了一种简单的方法来检索计算机的实际IP地址。 有了它,我将能够检测IP地址是否属于我的公司LAN,或者计算机是否通过另一个连接连接到Internet。 灵感来自这里:http://www.myengineeringworld.net/2014/12/get-public-ip-local-ip-mac-address-vba.html?m=1,最终代码如下

使用以下代码,我首先检测Outlook是联机还是脱机,并使用GetMyPublicIP功能检索IP地址

Sub enligne_horsligne()

'olCachedConnectedDrizzle   600     The account is using cached Exchange code such that headers are downloaded first, followed by the
                                'bodies and attachments of full items.
'olCachedConnectedFull      700     The account is using cached Exchange mode on a Local Area Network or a fast connection with the Exchange server.
                                'The user can also select this state manually, disabling auto-detect logic and always downloading full items
                                'regardless of connection speed.
'olCachedConnectedHeaders   500     The account is using cached Exchange mode on a dial-up or slow connection with the Exchange server, such
                                'that only headers are downloaded. Full item bodies and attachments remain on the server.
                                'The user can also select this state manually regardless of connection speed.
'olCachedDisconnected       400     The account is using cached Exchange mode with a disconnected connection to the Exchange server.
'olCachedOffline            200     The account is using cached Exchange mode and the user has selected Work Offline from the File menu.
'olDisconnected             300     The account has a disconnected connection to the Exchange server.
'olNoExchange               0       The account does not use an Exchange server.
'olOffline                  100     The account is not connected to an Exchange server and is in the classic offline mode.
                                'This also occurs when the user selects Work Offline from the File menu.
'olOnline                   800     The account is connected to an Exchange server and is in the classic online mode.

Dim myNamespace As Outlook.NameSpace
Dim adresse_IP As String
Dim mpfInbox As Outlook.folder

Set myNamespace = Application.GetNamespace("MAPI")

 If (myNamespace.ExchangeConnectionMode = 700 Or   myNamespace.ExchangeConnectionMode = 600 Or myNamespace.ExchangeConnectionMode   = 700) Then
    MsgBox ("Outlook is online")
    MsgBox (Environ("userdomain") & "" & Environ("username"))
 Else
    MsgBox ("Outlook is offline")
    MsgBox (Environ("userdomain") & "" & Environ("username"))
End If
adresse_IP = GetMyPublicIP()
MsgBox ("The IP adrress is  " & adresse_IP)
End Sub

函数 GetMyPublicIP 是

Function GetMyPublicIP() As String
'source:  http://www.myengineeringworld.net/2014/12/get-public-ip-local-ip-mac-address-vba.html?m=1
Dim HttpRequest As Object
On Error Resume Next
'Create the XMLHttpRequest object.
Set HttpRequest = CreateObject("MSXML2.XMLHTTP")
'Check if the object was created.
If Err.Number <> 0 Then
    'Return error message.
    GetMyPublicIP = "Could not create the XMLHttpRequest object!"
    'Release the object and exit.
    Set HttpRequest = Nothing
    Exit Function
End If
On Error GoTo 0
'Create the request - no special parameters required.
HttpRequest.Open "GET", "http://myip.dnsomatic.com", False
'Send the request to the site.
HttpRequest.Send
'Return the result of the request (the IP string).
GetMyPublicIP = HttpRequest.ResponseText
End Function

最新更新