Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4342|回复: 8

[已解决]宏可以设置电脑的ip吗??

[复制链接]
发表于 2011-4-23 16:42 | 显示全部楼层 |阅读模式
5学分
如题,不知道可以实现用宏设置本地连接里面的ip不,希望可以实现
ip设置如下:
ip.JPG

另,如果可以设置,那有怎么取消上面的设置,而让ip全部显示为“自动获得”
最佳答案
2011-4-23 19:45
Sub Set_Static()
    Dim objWMIService, colNetAdapters, objNetAdapter
    Dim strIPAddress, strSubnetMask, strGateway, strGatewaymetric, strDNS
    Dim errEnable, errGateways, errDNS
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration " _
            & "where IPEnabled=TRUE")
    strIPAddress = Array("192.168.1.101")
    strSubnetMask = Array("255.255.255.0")
    strGateway = Array("192.168.1.1")
    strGatewaymetric = Array(1)
'    strDNS = Array("10.10.10.10", "10.10.10.11")
    For Each objNetAdapter In colNetAdapters
        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
        errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
    Next
End Sub

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2011-4-23 19:16 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2011-4-23 19:23 | 显示全部楼层
回复

使用道具 举报

发表于 2011-4-23 19:40 | 显示全部楼层
高难度问题了,搞不懂了
回复

使用道具 举报

发表于 2011-4-23 19:40 | 显示全部楼层
高难度问题了,搞不懂了
回复

使用道具 举报

发表于 2011-4-23 19:45 | 显示全部楼层    本楼为最佳答案   
Sub Set_Static()
    Dim objWMIService, colNetAdapters, objNetAdapter
    Dim strIPAddress, strSubnetMask, strGateway, strGatewaymetric, strDNS
    Dim errEnable, errGateways, errDNS
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery _
        ("Select * from Win32_NetworkAdapterConfiguration " _
            & "where IPEnabled=TRUE")
    strIPAddress = Array("192.168.1.101")
    strSubnetMask = Array("255.255.255.0")
    strGateway = Array("192.168.1.1")
    strGatewaymetric = Array(1)
'    strDNS = Array("10.10.10.10", "10.10.10.11")
    For Each objNetAdapter In colNetAdapters
        errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
        errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
        errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
    Next
End Sub

评分

参与人数 2 +24 收起 理由
wbzxz + 6 乖乖,一点都看不懂。
xdwy81129 + 18 高难度的问题答案,谢谢

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2011-4-23 21:08 | 显示全部楼层
回复 吕?布 的帖子

太感谢吕布大侠了,还差一个宏,就是去掉ip设置,设置成自动


未命名.JPG
感谢
回复

使用道具 举报

发表于 2011-4-23 21:58 | 显示全部楼层
相当专业啊。
回复

使用道具 举报

发表于 2011-4-25 10:09 | 显示全部楼层
Sub Set_AutoGetIP()
' 设置为自动获取IP
    Dim objWMIService, colNetAdapters, objNetAdapter
    Dim lDHCP As Long
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
    Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

    For Each objNetAdapter In colNetAdapters
        lDHCP = objNetAdapter.EnableDHCP()    ' 自动获得IP 地址
        If lDHCP > 0 Then Debug.Print objNetAdapter.MACAddress & "设为自动获得IP地址"
    Next
End Sub

评分

参与人数 1 +18 收起 理由
xdwy81129 + 18 强悍的答案,名副其实的战神

查看全部评分

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-20 14:09 , Processed in 0.455309 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表