Reputation: 2450
I can toggle the internet proxy on Internet Explorer by going to Internet options
> Connections
> LAN settings
> Proxy Server
> Use a proxy server for your LAN
.
When I do that it toggles the registry key HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable
.
The problem is that it's not just that because when I change the value manually with regedit
it doesn't toggle the proxy nor the proxy option on IE.
I'm trying to be able to toggle it from Excel with VBA code.
Private Sub DisableProxy()
Dim Shell As New WshShell
Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"
End Sub
I tried using a program to monitor registry changes but it only shows that one key.
How can I toggle the proxy correctly from Excel VBA?
Upvotes: 2
Views: 1244
Reputation: 2450
With the help of this answer I found that I have to tell that IE's configuration settings changed but the code wasn't in VBA so I looked it up and found this site.
So after changing the code it worked.
' Reference:
' Windows Script Host Object Model
Private Declare Function InternetSetOptionA Lib "wininet.dll" (ByVal hInternet As Long, ByVal lOption As Long, ByVal sBuffer As String, ByVal lBufferLength As Long) As Integer
Private Sub ToggleProxy(ByVal Toggle As Integer)
Dim Shell As WshShell
Const INTERNET_OPTION_SETTINGS_CHANGED As Long = 39
If (Toggle <> 0 And Toggle <> 1) Then Exit Sub
Set Shell = New WshShell
Shell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", Toggle, "REG_DWORD"
InternetSetOptionA 0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0
End Sub
Private Sub Main()
ToggleProxy Toggle:=0 ' Disable
ToggleProxy Toggle:=1 ' Enable
End Sub
Upvotes: 3