Option Explicit'首先引用Microsoft Shell Controls And AutomationPrivate Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean On Error Resume Next Dim mShell As New Shell32.Shell Dim NetConnection As Shell32.Folder Dim FolderItem As Shell32.FolderItem Dim NetConnectionItem As ShellFolderItem Dim verb As Shell32.FolderItemVerb Set NetConnection = mShell.NameSpace(49) '这个49是我找出来的,有了它就可以避免遍历控制面板 If ObjPtr(NetConnection) = 0 Then ExcNetLinkMenu = False GoTo exitfunction End If Dim flag As Boolean flag = False For Each FolderItem In NetConnection.Items If FolderItem.Name = AdapterName Then Set NetConnectionItem = FolderItem flag = True Exit For End If Next If flag = False Then ExcNetLinkMenu = False GoTo exitfunction End If For Each verb In NetConnectionItem.Verbs If verb.Name = MenuName Then flag = True verb.DoIt ExcNetLinkMenu = True GoTo exitfunction End If Next If flag = False Then ExcNetLinkMenu = False GoTo exitfunction End Ifexitfunction: Set mShell = Nothing Set NetConnection = Nothing Set FolderItem = Nothing Set NetConnectionItem = Nothing Set verb = NothingEnd Function
Private Sub Command1_Click() Dim flag As Boolean '把 本地连接 2 换成你要控制的本地连接的名字 flag = ExcNetLinkMenu("本地连接", "停用(&B)") '这个在2000下对应的是禁用,具体是什么,点右键,自己看吧End Sub
Private Sub Command2_Click() '把 本地连接 2 换成你要控制的本地连接的名字 Dim flag As Boolean flag = ExcNetLinkMenu("本地连接", "启用(&A)")End Sub