Cài Đặt IP Máy Tính Bằng Code

Liên hệ QC

Kiều Mạnh

I don't program, I beat code into submission!!!
Tham gia
9/6/12
Bài viết
5,424
Được thích
4,036
Giới tính
Nam
Mình đang Tìm cách cài đặt IP Máy tính bằng code chạy từ Excel mà chưa Nghỉ ra được .... Tìm trên GPE hoài mà chưa thấy ..... vậy mình muốn hỏi có cách nào Cài đặt IP máy tính theo Hình sau Bằng Code
Bạn nào biết xin chỉ dùmIP.png
 
Máy Em cũng vậy chạy thành công Run As ....
Em muốn hỏi có cách nào ta viết code kiểm tra được nếu là "Wi-Fi" thì Set nwName="Wi-Fi" khác thì là Set nwName="Local Area Connection"
Không Anh .............ý Em muốn hỏi viết thành hàm Bao quát nhất xài cho 2 trường hợp đó

Cảm ơn Anh
Thử với hàm này xem:
Mã:
Function GetWirlessName()
Dim strKeyPath
Dim strComputer
Dim objReg
Dim arrSubKeys
Dim SubKey
Dim strValueName
Dim dwValue
Dim strValue
Const HKLM = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objReg.Enumkey HKLM, strKeyPath, arrSubKeys
For Each SubKey In arrSubKeys
    strValueName = "MediaSubType"
    objReg.GetDWORDValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, dwValue
    If dwValue = 2 Then
        strValueName = "Name"
        objReg.GetStringValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, strValue
        Exit For
    End If
Next
GetWirlessName = strValue
End Function
Mọi thông số được lấy từ Registry.
Tôi không có điều kiện test trên nhiều máy (kết nối không dây và có dây) nên không chắc lắm
 
Upvote 0
Thử với hàm này xem:
Mã:
Function GetWirlessName()
Dim strKeyPath
Dim strComputer
Dim objReg
Dim arrSubKeys
Dim SubKey
Dim strValueName
Dim dwValue
Dim strValue
Const HKLM = &H80000002
strKeyPath = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}"
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
objReg.Enumkey HKLM, strKeyPath, arrSubKeys
For Each SubKey In arrSubKeys
    strValueName = "MediaSubType"
    objReg.GetDWORDValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, dwValue
    If dwValue = 2 Then
        strValueName = "Name"
        objReg.GetStringValue HKLM, strKeyPath & "\" & SubKey & "\" & "Connection", strValueName, strValue
        Exit For
    End If
Next
GetWirlessName = strValue
End Function
Mọi thông số được lấy từ Registry.
Tôi không có điều kiện test trên nhiều máy (kết nối không dây và có dây) nên không chắc lắm
Máy Em nó báo Empty
 
Upvote 0
Mạnh thử test code này xem thế nào
Mã:
Sub Test()
  Const NW_CONNECTION = &H31&
  Dim objShell As Object, item, objConn As Object
  Set objShell = CreateObject("Shell.Application")
  Set objConn = objShell.Namespace(NW_CONNECTION)
  For Each item In objConn.Items
    MsgBox item.Name
  Next
End Sub
Không chắc nên phải test trên nhiều máy, nhiều kiểu kết nối để rút ra kết luận
 
Upvote 0
Vậy thì bạn kiểm tra lại đường dẫn trong registry trên máy bạn, chỗ nào có "Local Area Connection" thì sửa lại cho phù hợp
Em Ctrl + F Local Area Connection thì nó cho ra như hình
khóa Sau
Computer\HKEY_LOCAL_MACHINE\SYSTEM\Setup\Upgrade\NetworkDriverBackup\Control\Network\{4d36e972-e325-11ce-bfc1-08002be10318}\{B8EFE705-08BC-465B-9979-E29EF62D1A12}\ConnectionCapture.PNG
 
Upvote 0
Mạnh thử test code này xem thế nào
Mã:
Sub Test()
  Const NW_CONNECTION = &H31&
  Dim objShell As Object, item, objConn As Object
  Set objShell = CreateObject("Shell.Application")
  Set objConn = objShell.Namespace(NW_CONNECTION)
  For Each item In objConn.Items
    MsgBox item.Name
  Next
End Sub
Không chắc nên phải test trên nhiều máy, nhiều kiểu kết nối để rút ra kết luận
máy Em Ok ... máy Desktop xài cáp Quang ... code chạy rất nhanh
Chạy Windows10_x32 ... Còn x64 Em ko có nên không Biết
Mới thử Windows7_x32 cũng vậy ... Tốt nhưng nó sẻ báo ở Máy LAN2 là 2 lần Msgbox:
1/ Local Area Connection
2/ Local Area Connection 2

Lần 2 Thêm số 2 cuối
 
Lần chỉnh sửa cuối:
Upvote 0
Em mới coi trên mạng thấy code này nó chạy cũng rất nhanh
Mã:
Private Sub Test()
    Dim oWMI, Instances, Instance
    Set oWMI = GetObject("WINMGMTS:\\.\ROOT\cimv2")
    Set Instances = oWMI.InstancesOf("Win32_NetworkAdapter")
    For Each Instance In Instances
        If Instance.NetconnectionID <> "null" Then
            MsgBox (Instance.NetconnectionID & "---" _
            & Instance.AdapterType & "-----" & Instance.NetConnectionStatus)
        End If
    Next
End Sub
 
Upvote 0
Anh @kieumanh thử dùng PowerShell xem sao...
Mạnh có Tìm hiểu nhiều về IP thấy trên GPE có code sau chạy tốt .......... Nhưng Mình thấy cách Viết hơi dài rồi Mình có thử nghiên Cứu Winsock Mình viết thành code sau rất Gọn vậy Nhờ Bạn test dùm trên Windows_x64 xem 2 code sau nó như thế nào với
1/ Code Copy trên GPE
PHP:
Private Sub GetIPAddress()
    Dim Item
    Dim rIP As String
    On Error Resume Next
    With GetObject("winmgmts:\\.\root\cimv2")
      For Each Item In .ExecQuery("Select * from Win32_NetworkAdapterConfiguration", , 48)
        Range("A65536").End(xlUp).Offset(1) = Item.IPAddress(0)
        'MsgBox (Item.IPAddress(0))
      Next
    End With
End Sub
2/ Code Mạnh Nghiên cứu Winsock để Viết
PHP:
Public Function GetIPAddress()
    GetIPAddress = CreateObject("MSWinsock.Winsock").LocalIp
    'MsgBox GetIPAddress
    Range("A65536").End(xlUp).Offset(1) = GetIPAddress
End Function
Xin cảm Ơn
 
Upvote 0
Upvote 0
Web KT
Back
Top Bottom