假设我已连接到test.server.com
我的 Internet 访问。有没有办法使用 VBA 返回该字符串?
为了扩展,应您的要求(抱歉,网络是我对网络知之甚少的东西):
以下是指 Windows 7 企业版: 如果您要导航到控制面板中的“网络和共享中心”,这将是“域网络”的名称。或者,单击任务栏中的网络图标将显示此网络名称,并在其下方显示“Internet 访问”。
您可以使用GetComputerNameEx从 Windows 基础 API获取 DNS 主机和/或域名:
Option Explicit
Enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDnsDomain
ComputerNamePhysicalDnsFullyQualified
End Enum
Declare Function GetComputerNameEx Lib "kernel32" Alias "GetComputerNameExA" ( _
ByVal NameType As COMPUTER_NAME_FORMAT, _
ByVal lpBuffer As String, _
ByRef lpnSize As Long) As Long
Sub test()
Dim buffer As String
Dim size As Long
size = 255
buffer = Space(size)
GetComputerNameEx ComputerNameDnsFullyQualified, buffer, size
Debug.Print Left$(buffer, size)
End Sub
使用用户 A. Webb 的回复,我使用以下方法让它工作,它提取整个计算机名称和网络,然后将其缩减。我敢肯定有更漂亮的方法,但它的工作原理。
Option Explicit
Enum COMPUTER_NAME_FORMAT
ComputerNameNetBIOS
ComputerNameDnsHostname
ComputerNameDnsDomain
ComputerNameDnsFullyQualified
ComputerNamePhysicalNetBIOS
ComputerNamePhysicalDnsHostname
ComputerNamePhysicalDnsDomain
ComputerNamePhysicalDnsFullyQualified
End Enum
Declare Function GetComputerNameEx Lib "kernel32" Alias "GetComputerNameExA" ( _
ByVal NameType As COMPUTER_NAME_FORMAT, _
ByVal lpBuffer As String, _
ByRef lpnSize As Long) As Long
Sub test()
Dim buffer As String
Dim size As Long
Dim network_and_computer As String
Dim network_name As String
size = 255
buffer = Space(size)
GetComputerNameEx ComputerNameDnsFullyQualified, buffer, size
network_and_computer = Left$(buffer, size)
MsgBox network_and_computer
network_name = Right(network_and_computer, Len(network_and_computer) - InStr(1, network_and_computer, ".", vbTextCompare))
MsgBox network_name
End Sub