Excel精英培训网

 找回密码
 注册

VBA获取当前计算机名的几种方法(转)

已有 404 次阅读2011-1-28 11:34 |

在学习中总结了几个获取计算机名称的几种方法,贴出来分享一下。

1、函数法:

           MsgBox Environ("COMPUTERNAME")

2、利用外部链接库

             MsgBox CreateObject("WScript.Network").computername

或者:

             MsgBox CreateObject("Shell.LocalMachine").MachineNaMe

3、调用API函数

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Sub computer_name()
      Dim computername As String * 255
      GetComputerName computername, 255&
      MsgBox computername
End Sub

4、读取注册表
Sub computer_name1()
    MsgBox CreateObject("wscript.shell").regread("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName")
End Sub

5、调用WMI脚本

Sub computer_name2()
    Set obj = GetObject("winmgmts:\\.\root\CIMV2")
    Set objitm = obj.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    For Each itm In objitm
        MsgBox itm.Name
    Next
End Sub

评论 (0 个评论)

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-21 20:05 , Processed in 0.150073 second(s), 5 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

返回顶部