找回密码
 立即注册
搜索

VBA获取U盘、主板、CPU序列号和网卡MAC地址

1
回复
935
查看
[复制链接]

2

主题

11

帖子

51

幻币

江湖少侠

Rank: 2

积分
148
QQ
2017-4-5 18:59:52 显示全部楼层 |阅读模式
      '方法1
Sub Auto_Open()
On Error Resume Next
Set fs = CreateObject("Scripting.FileSystemObject")
StrDrive = "B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
StrDriveArray = Split(StrDrive, ",")
For StartPos = 1 To UBound(StrDriveArray)
Set d = fs.GetDrive(fs.GetDriveName(fs.GetAbsolutePathName(StrDriveArray(StartPos) & ":")))
If d.DriveType = 1 Then
s = d.SerialNumber
Exit For
End If
Next
If s  "" Then
Range("Sheet1!d8") = s
Else
Range("Sheet1!d8") = "系统未检测到U盘!"
End If
Set d = Nothing
Set fs = Nothing
Call QueryOther
End Sub
'方法2
Sub DetectUdisk()
On Error Resume Next
Set objWMIService = GetObject("winmgmts:.rootcimv2")
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 2")
For Each objDisk In colDisks
RemovableDrive = objDisk.DeviceID
If CreateObject("Scripting.FileSystemObject").GetDrive(RemovableDrive).IsReady Then
s = CreateObject("Scripting.FileSystemObject").GetDrive(RemovableDrive).SerialNumber
Exit For
End If
Next
If s  "" Then
Range("Sheet1!d8") = s
Else
Range("Sheet1!d8") = "系统未检测到U盘!"
End If
Call QueryOther
End Sub
Sub QueryOther()
'2007.1.19 更新,获取主板序列号, CPUID, 网卡MAC地址
Set objWMIService = GetObject("winmgmts:.rootcimv2")
Set colItems = objWMIService.ExecQuery("Select SerialNumber From Win32_BIOS")
For Each objItem In colItems
Range("Sheet1!E8") = objItem.SerialNumber
Exit For
Next
Set colItems = Nothing

Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor")
For Each objItem In colItems
Range("Sheet1!F8") = objItem.ProcessorId
Exit For
Next
Set colItems = Nothing

Set colItems = objWMIService.ExecQuery("SELECT MACAddress FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer  'Microsoft'))")
For Each objItem In colItems
Range("Sheet1!G8") = objItem.MACAddress
Exit For
Next
Set colItems = Nothing
End Sub

PPT学习论坛
回复

使用道具 举报

4

主题

113

帖子

472

幻币

豪侠武师

Rank: 4

积分
918
QQ
2017-4-5 21:02:22 显示全部楼层
很不错,找到组织了!
PPT学习论坛
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册