4
我需要將IP地址提取到VBA宏。此代碼可以正常工作,但命令對話只是短暫可見的,這不是一個好看的樣子。我可以使用修改來「靜默」嗎?Word VBA檢索IP地址「靜默」
Sub getIP()
Set objShell = CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("%comspec% /c ipconfig.exe")
Do Until objExecObject.StdOut.AtEndOfStream
strLine = objExecObject.StdOut.ReadLine()
strIP = InStr(strLine, "Address")
If strIP <> 0 Then
IPArray = Split(strLine, ":")
strIPAddress = IPArray(1)
End If
Loop
SynapseForm.LabelIP.Caption = strIPAddress
End Sub
更新,發現使用Wscript.Shell寫入到一個臨時文件變型,該作品「靜靜地」不是像你一樣Remou的下面
Sub getIPAddress()
Dim IP_Address: IP_Address = GetIP()
If IP_Address = "0.0.0.0" Or IP_Address = "" Then
MsgBox "No IP Address found.", , ""
Else
MsgBox IP_Address
'MsgBox IP_Address, , "IP address"
End If
End Sub
Function GetIP()
Dim ws: Set ws = CreateObject("WScript.Shell")
Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim TmpFile: TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
Dim ThisLine, IP
If ws.Environment("SYSTEM")("OS") = "" Then
ws.Run "winipcfg /batch " & TmpFile, 0, True
Else
ws.Run "%comspec% /c ipconfig > " & TmpFile, 0, True
End If
With fso.GetFile(TmpFile).OpenAsTextStream
Do While Not .AtEndOfStream
ThisLine = .ReadLine
If InStr(ThisLine, "Address") <> 0 Then
IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
End If
Loop
.Close
End With
'WinXP (NT? 2K?) leaves a carriage return at the end of line
If IP <> "" Then
If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
End If
GetIP = IP
fso.GetFile(TmpFile).Delete
Set fso = Nothing
Set ws = Nothing
End Function
您應該在此處發佈重要部分的代碼,並附上適當的歸屬。 – 2011-02-11 18:52:21
ARG這是相當大的。我如何將它稱爲宏?我不需要機器名稱,並且每臺機器只有一個網絡地址,我需要真正以字符串的形式訪問IP。 – Saul 2011-02-11 21:49:46