我正在尝试为MS Excel编写VB脚本,以从域中获取IP地址。我目前使用ping,但是它太慢了,最多需要19秒。
我创建了一个特定的功能来做到这一点:
Public Function RunShell(Url As String) As String
Dim ReplacedURL As String
ReplacedURL = Replace(Url,"https://","")
ReplacedURL = Replace(ReplacedURL,"http://","")
If InStr(ReplacedURL,"/") > 0 Then
ReplacedURL = Mid(ReplacedURL,1,InStr(ReplacedURL,"/") - 1)
End If
Dim Command As String
Command = "cmd /c """ & "ping " & ReplacedURL & "|clip"""
CreateObject("WScript.Shell").Run Command,True
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
Dim CommandOutput As String
CommandOutput = DataObj.GetText
Dim IPAddress As String
IPAddress = ""
If InStr(CommandOutput,"[") > 0 And InStr(CommandOutput,"]") > 0 Then
IPAddress = Mid(CommandOutput,InStr(CommandOutput,"[") + 1,"]") - 1 - (InStr(CommandOutput,"[")))
Else
Err.Raise ERR_WRONG_URL
End If
RunShell = IPAddress
End Function
是否有更好的方法在不到几秒钟的时间内从域获取IP地址?