Reputation: 13
I need a little help with a VBScript I am using to disable and enable the proxy settings. What I would like to happen is the script tells the user what the current setting for the proxy is either off or on then if they want it to they can click would you like to change yes/no. Or I would like it to say the proxy is now off or the proxy is now on.
I know how to make a message box I just dont know where I should put the code.
This is my text box code:
result = Msgbox("Proxy is now set to off", vbOKonly+vbInformation, "")
And this is the proxy changing code:
Option Explicit
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")
'Determine current proxy setting and toggle to oppisite setting
strSetting = WSHShell.RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable")
If strSetting = 1 Then
NoProxy
Else
End If
'Subroutine to Toggle Proxy Setting to ON
Sub Proxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 1, "REG_DWORD"
End Sub
'Subroutine to Toggle Proxy Setting to OFF
Sub NoProxy
WSHShell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub
Upvotes: 1
Views: 87
Reputation: 16680
I've reorganised your code a bit but this should allow you to choose if you wish to toggle the Proxy on or off.
Some of the changes include;
Constant
.Main()
. We then make sure that the call to that procedure is the only code we run in the global scope except for the WScript.Shell
declaration.CurrentProxy()
to a Boolean makes toggling the off and on behaviour easier.Array
to store variations of words used in the MsgBox()
to avoid duplication of code.Option Explicit
Dim WSHShell, strSetting
Set WSHShell = WScript.CreateObject("WScript.Shell")
'Store strings that will not change and are reused in constants
Const APPNAME = "Proxy Setting"
Const PROXY_SETTING = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable"
'Have main procedure to be single point of script execution.
Call Main()
Sub Main()
'Convert return value to Boolean to make it easy to do toggle.
Dim setting: setting = CBool(CurrentProxy())
Dim state
'Use array to store wordy bits that will be used by the Message Box.
If setting Then
state = Array("enabled", "off")
Else
state = Array("disabled", "on")
End If
If MsgBox("Proxy is " & state(0) & vbCrLf & "Do you wish to switch it " & state(1), vbYesNo + vbQuestion, APPNAME) = vbYes Then
'Toggle is opposite of current state so use Not.
Call ToggleProxy(Not setting)
Call MsgBox("Proxy has switched " & state(1), vbInformation, APPNAME)
End If
End Sub
'Determine current proxy setting and toggle to oppisite setting
Function CurrentProxy()
Dim strSetting
strSetting = WSHShell.RegRead(PROXY_SETTING)
CurrentProxy = strSetting
End Function
'Joined Proxy and NoProxy together into one procedure call and pass in the
'ProxyEnable setting as an argument.
'Subroutine to Toggle Proxy Setting to ON
Sub ToggleProxy(setting)
'Abs() function makes sure we only pass back 1 or 0 as Boolean True in
'VBScript is actually -1.
WSHShell.RegWrite PROXY_SETTING, Abs(setting), "REG_DWORD"
End Sub
Upvotes: 1