Kiều Mạnh
I don't program, I beat code into submission!!!
- Tham gia
- 9/6/12
- Bài viết
- 5,538
- Được thích
- 4,133
- Giới tính
- Nam
Chắc kiểu thế này: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ùmView attachment 180287
Private Sub SetIp(nwName As String, varIp As String, varSm As String, _
varGw As String, varDNS1 As String, varDNS2 As String)
Dim sComm1 As String, sComm2 As String, sComm3 As String, sComm4 As String
On Error Resume Next
sComm1 = "netsh interface ip set address name=""" & nwName & """ source=static addr=" & varIp & " mask=" & varSm
sComm2 = "netsh interface ip set address name=""" & nwName & """ gateway=" & varGw & " gwmetric=0"
sComm3 = "netsh interface ip set dns name=""" & nwName & """ source=static addr=" & varDNS1
sComm4 = "netsh interface ip add dns name=""" & nwName & """ addr=" & varDNS2
With CreateObject("Wscript.Shell")
.Run "cmd /c " & sComm1, 0, True
.Run "cmd /c " & sComm2, 0, True
.Run "cmd /c " & sComm3, 0, True
.Run "cmd /c " & sComm4, 0, True
End With
MsgBox "Finish"
End Sub
Set nwName="Wireless Network Connection"
Set varIp=211.54.133.251
Set varSm=255.255.252.0
Set varGw=211.54.135.253
Set varDNS1=211.54.128.1
Set varDNS2=211.54.135.251
netsh interface ip set address name=%nwName% source=static addr=%varIp% mask=%varSm%
netsh interface ip set address name=%nwName% gateway=%varGw% gwmetric=0
netsh interface ip set dns name=%nwName% source=static addr=%varDNS1%
netsh interface ip add dns name=%nwName% addr =%varDNS2%
Em mới lục trên máy Em có File này chắc Code Anh Viết Sao Em chạy không Thấy được ...hay em làm sai cái gìChắc kiểu thế này:
Với các đối số đầu vào bạn tự truyền vào, chẳng hạn như hình:Mã:Private Sub SetIp(nwName As String, varIp As String, varSm As String, _ varGw As String, varDNS1 As String, varDNS2 As String) Dim sComm1 As String, sComm2 As String, sComm3 As String, sComm4 As String On Error Resume Next sComm1 = "netsh interface ip set address name=""" & nwName & """ source=static addr=" & varIp & " mask=" & varSm sComm2 = "netsh interface ip set address name=""" & nwName & """ gateway=" & varGw & " gwmetric=0" sComm3 = "netsh interface ip set dns name=""" & nwName & """ source=static addr=" & varDNS1 sComm4 = "netsh interface ip add dns name=""" & nwName & """ addr=" & varDNS2 With CreateObject("Wscript.Shell") .Run "cmd /c " & sComm1, 0, True .Run "cmd /c " & sComm2, 0, True .Run "cmd /c " & sComm3, 0, True .Run "cmd /c " & sComm4, 0, True End With MsgBox "Finish" End Sub
View attachment 180289
Hoặc có thể tạo file .bat với nội dung:
6 dòng trên cùng bạn tự thiết lậpMã:Set nwName="Wireless Network Connection" Set varIp=211.54.133.251 Set varSm=255.255.252.0 Set varGw=211.54.135.253 Set varDNS1=211.54.128.1 Set varDNS2=211.54.135.251 netsh interface ip set address name=%nwName% source=static addr=%varIp% mask=%varSm% netsh interface ip set address name=%nwName% gateway=%varGw% gwmetric=0 netsh interface ip set dns name=%nwName% source=static addr=%varDNS1% netsh interface ip add dns name=%nwName% addr =%varDNS2%
Có 2 vấn đề cần phải để ý:Em mới lục trên máy Em có File này chắc Code Anh Viết Sao Em chạy không Thấy được ...hay em làm sai cái gì
Sau khi chạy code xong Em dùng File *.vbs kiểm tra vẫn vậy
Em mới thử nó ra vầy.... Em sử Dụng Cáp Quang ViettelCó 2 vấn đề cần phải để ý:
1> Connection Name (có thể không phải là "Wireless Network Connection")
2> RunAs Admintrator
Để biết thông tin về Connection Name và nhũng thứ khác, bạn làm như sau:
- Bấm tổ hợp phím Windows + R và gõ cmd ---> Enter
- Gõ lệnh NETSH và Enter
- Gõ tiếp Interface IP Show Config và Enter
Còn liên quan đến vụ chạy RunAs thì chắc bạn biết rồi
Vậy thì Connection Name là "Local Area Connection"Em mới thử nó ra vầy.... Em sử Dụng Cáp Quang Viettel View attachment 180299
Thì sửa tham số truyền nwName đấy, cho nó = "Local Area Connection"Vậy code Em sửa lại khúc nào Anh
Set nwName="Wi-Fi"
Set varIp=192.168.1.15
Set varSm=255.255.255.0
Set varGw=192.168.1.1
netsh interface ip set address name=%nwName% source=static addr=%varIp% mask=%varSm% gateway=%varGw%
PAUSE
Máy Em cũng vậy chạy thành công Run As ....Thì sửa tham số truyền nwName đấy, cho nó = "Local Area Connection"
---------------------
Trên máy tôi thì tên là "Wi-Fi". Tôi vừa thử chạy lệnh:
Và đã thành côngMã:Set nwName="Wi-Fi" Set varIp=192.168.1.15 Set varSm=255.255.255.0 Set varGw=192.168.1.1 netsh interface ip set address name=%nwName% source=static addr=%varIp% mask=%varSm% gateway=%varGw% PAUSE
(đương nhiên RunAs Administrator)
Thử cái này được không anhMá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
Sub vidu()
Dim sCmd As String, ConnName As String, strTMP
sCmd = "CMD /C powershell Get-NetIPConfiguration | clip"
'CreateObject("WScript.Shell").Run sCmd, 0, True '
Shell sCmd, vbHide
strTMP = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")
strTMP = Split(strTMP, Chr(13))
For i = LBound(strTMP) To UBound(strTMP)
If InStr(strTMP(i), ":") Then
ConnName = Trim(Split(strTMP(i), ":")(1))
Exit For
End If
Next i
MsgBox ConnName
End Sub
Không được và chạy rất chậmThử cái này được không anh
PHP:Sub vidu() Dim sCmd As String, ConnName As String, strTMP sCmd = "CMD /C powershell Get-NetIPConfiguration | clip" 'CreateObject("WScript.Shell").Run sCmd, 0, True ' Shell sCmd, vbHide strTMP = CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") strTMP = Split(strTMP, Chr(13)) For i = LBound(strTMP) To UBound(strTMP) If InStr(strTMP(i), ":") Then ConnName = Trim(Split(strTMP(i), ":")(1)) Exit For End If Next i MsgBox ConnName End Sub
oh mới thử lại hiểu rồi ....Sao chay hồi nảy chạy chậm ko hiểuHic! Em thử với WScript.Shell thì chậm với shell function thì không chậm
Cái sub của em là để lấy Connection Name đang được kết nối mạng.
Anh thử lại khúc này xem.oh mới thử lại hiểu rồi ....Sao chay hồi nảy chạy chậm ko hiểu
Sub vidu()
Dim sCmd As String, ConnName As String, strTMP
Dim pText As String, delpath As String
pText = ThisWorkbook.Path & "\oText.txt"
delpath = "CMD /C DEL /f /q """ & pText & """"
sCmd = "CMD /C powershell Get-NetIPConfiguration > " & pText
Shell delpath, vbHide
Shell sCmd, vbHide
strTMP = CreateObject("Scripting.FileSystemObject").OpenTextFile(pText).ReadAll()
Shell delpath, vbHide
strTMP = Split(strTMP, Chr(13))
For i = LBound(strTMP) To UBound(strTMP)
If InStr(strTMP(i), ":") Then
ConnName = Trim(Split(strTMP(i), ":")(1))
Exit For
End If
Next i
MsgBox ConnName
End Sub
Cứ cái nào đang kết nối thì lấy cái đó á anh.Sub đó lấy được trường hợp 1 hay 2
Chạy lỗi .......... Link sau có các Lệnh liên quan hay Tuy nhiên coi khúc biết khúc koAnh thử lại khúc này xem.
Em dùng SSD nên cứ chạy 2 lần liên tiếp gần nhau thì 1 lần bị lỗiPHP:Sub vidu() Dim sCmd As String, ConnName As String, strTMP Dim pText As String, delpath As String pText = ThisWorkbook.Path & "\oText.txt" delpath = "CMD /C DEL /f /q """ & pText & """" sCmd = "CMD /C powershell Get-NetIPConfiguration > " & pText Shell delpath, vbHide Shell sCmd, vbHide strTMP = CreateObject("Scripting.FileSystemObject").OpenTextFile(pText).ReadAll() Shell delpath, vbHide strTMP = Split(strTMP, Chr(13)) For i = LBound(strTMP) To UBound(strTMP) If InStr(strTMP(i), ":") Then ConnName = Trim(Split(strTMP(i), ":")(1)) Exit For End If Next i MsgBox ConnName End Sub
ở dòng strTMP = CreateObject("Scripting.FileSystemObject").OpenTextFile(pText).ReadAll()
Cứ cái nào đang kết nối thì lấy cái đó á anh.
Thử với hàm này xem: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
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áy Em nó báo EmptyThử với hàm này xem:
Mọi thông số được lấy từ Registry.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
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
Trong cái đống đó chắc bạn cần lệnh này: netsh int show interfaceChạy lỗi .......... Link sau có các Lệnh liên quan hay Tuy nhiên coi khúc biết khúc ko
https://ss64.com/nt/netsh.html
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ợpMáy Em nó báo Empty
Chỗ màu vàng theo link anh gửi (https://ss64.com/nt/netsh.html) có đó:Nó thiếu theo Hình màu vàng
netsh interface ipv4 add dns "Wi-fi" 8.8.8.8 'Connection Name = "Wi-Fi"
netsh interface ipv4 add dns "Wi-fi" 8.8.4.4 index=2
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
Em Ctrl + F Local Area Connection thì nó cho ra như hìnhVậ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
máy Em Ok ... máy Desktop xài cáp Quang ... code chạy rất nhanhMạnh thử test code này xem thế nào
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ậnMã: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
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
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ớiAnh @kieumanh thử dùng PowerShell xem sao...
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
Public Function GetIPAddress()
GetIPAddress = CreateObject("MSWinsock.Winsock").LocalIp
'MsgBox GetIPAddress
Range("A65536").End(xlUp).Offset(1) = GetIPAddress
End Function
Thử tìm trong /Windows nhưng không thấy file MSWINSCK.OCXvậy là x64 .......... Winsock nó tịt rồi![]()
sao hay vậy ...Mạnh xem tối qua rồi ...........Lỗi ...........ko biết tại sao đang để đó chưa coi lạiThử tìm trong /Windows nhưng không thấy file MSWINSCK.OCX
Anh Mạnh thử cái này xem.
http://www.keysight.com/main/editor...0001131:epsg:sud&nid=-11143.0.00&lc=eng&cc=VN