如何在 Excel VBA 中获取当前网络的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14402918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get the name of the current network in Excel VBA?
提问by seegoon
Say I'm connected to test.server.com
for my Internet access. Is there any way to have that string returned using VBA?
假设我已连接到test.server.com
Internet 访问。有什么办法可以使用 VBA 返回该字符串吗?
To expand, at your requests (sorry, networking is something I know very little about):
根据您的要求进行扩展(抱歉,我对网络知之甚少):
The following refers to Windows 7 Enterprise: If you were to navigate to the 'Network and Sharing Center' within the Control Panel, this would be the name of the 'Domain network'. Alternatively, clicking on the networking icon in the taskbar would show this network name with 'Internet access' beneath it.
以下是针对 Windows 7 Enterprise:如果您要导航到控制面板中的“网络和共享中心”,这将是“域网络”的名称。或者,单击任务栏中的网络图标会显示此网络名称,其下方带有“Internet 访问”。
回答by A. Webb
You can get the DNS host and/or domain name with GetComputerNameExfrom the Windows base API:
您可以使用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
回答by seegoon
Using user A. Webb's reply, I got it working using the following, which pulls the whole computer name and network and then just pares it down. I'm sure there are prettier methods, but it works.
使用用户 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