我有一个包含距离计算的Google地图宏。
如果有互联网连接我只想要这个,否则它应该显示一条消息“没有可用的互联网连接,因此无法进行距离计算”。然后,当用户点击“确定”时,它应该仍然执行除距离计算之外的其余宏,并说明该字段“无可用距离”。
子公式为:
Sub Calculate()
Application.ScreenUpdating = False
' Calculate Macro
Range("P32:Y32").Select
ActiveCell.FormulaR1C1 = "=Calculation!R[-17]C[-7]"
Range("P33:Y34").Select
ActiveCell.FormulaR1C1 = "=Calculation!R[-16]C[-7]"
Range("A100").Select
If IsInternetConnected() = True Then
If Range("V45") > 1 Then
Dim wks1 As Worksheet
Dim blnDone As Boolean, strBaseAddr As String, strGuestAddr As String
Dim strDist As String, p As Integer
Set wks1 = Application.ActiveSheet
blnDone = False
strBaseAddr = Range("W43")
wks1.Activate
wks1.Range("W44").Select
While Not blnDone
If Len(ActiveCell) <> 0 Then
blnDone = False
strDestAddr = ActiveCell
strDist = gDistance(strDestAddr, strBaseAddr)
p = 1
While strDist = "OVER_QUERY_LIMIT" And p <= 5 'if over limit more than 5 times, move on
' try one more time after a pause
Application.Wait (Now() + #12:00:02 AM#) 'wait 2 seconds.. should be long enough but at times isn't.
strDist = gDistance(strDestAddr, strBaseAddr)
p = p + 1
Wend
If Not IsNumeric(strDist) Then
strDist = 0
End If
Range("X43") = strDist 'distance column
ActiveCell.Offset(1, 0).Select 'move down one row
Else
blnDone = True
End If
Wend
Else
Range("X45") = "No distance available"
Else
MsgBox "No internet connection is detected!"
End If
End If
Application.ScreenUpdating = True
End Sub
和互联网连接的功能是:
Private Declare Function InternetGetConnectedState _
Lib "wininet.dll" (ByRef dwflags As Long, _
ByVal dwReserved As Long) As Long
Private Const INTERNET_CONNECTION_MODEM As Long = &H1
Private Const INTERNET_CONNECTION_LAN As Long = &H2
Private Const INTERNET_CONNECTION_PROXY As Long = &H4
Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20
Function IsInternetConnected() As Boolean
Dim L As Long
Dim R As Long
R = InternetGetConnectedState(L, 0&)
If R = 0 Then
IsInternetConnected = False
Else
If R <= 4 Then
IsInternetConnected = True
Else
IsInternetConnected = False
End If
End If
End Function