Advertisement
YasserKhalil2019

T3818_Change Proxy Settings Using VBA

Jul 15th, 2019
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. https://excel-egy.com/forum/t3818
  2. ---------------------------------
  3.  
  4. #If VBA7 Then
  5. Private Declare PtrSafe Function InternetSetOptionA Lib "wininet.dll" (ByVal hInternet As Long, ByVal lOption As Long, ByVal sBuffer As String, ByVal lBufferLength As Long) As Integer
  6. #Else
  7. 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
  8. #End If
  9.  
  10. Sub Change_Proxy_Settings_Using_VBA()
  11. Dim vUser1, vUser2, ans%
  12.  
  13. ans = MsgBox("To Enable Proxy Settings Click 'Yes'." & Chr(13) & "To Disable Proxy Settings Click 'No'", vbYesNoCancel + vbQuestion)
  14.  
  15. If ans = vbYes Then
  16. vUser1 = InputBox("Enter The Proxy Server You Want To Use.", "Proxy Server", "http://excel-egy.com:8080")
  17. If vUser1 = "" Then Exit Sub
  18.  
  19. Rem Proxy Server Exceptions
  20. MsgBox "The URLs You Would Like To Override Should Be In Cell A1. Use Semi-Colons (;) To Separate Entries", 64
  21. vUser2 = ActiveSheet.Range("A1").Value
  22.  
  23. ProxySettings True, vUser1, vUser2
  24. ElseIf ans = vbNo Then
  25. ProxySettings False
  26. Else
  27. Exit Sub
  28. End If
  29. End Sub
  30.  
  31. Sub ProxySettings(bEnable As Boolean, Optional sProxyServer, Optional sProxyOverride)
  32. Dim objShell As Object, regPath$, regServer$, regOverride$, regEnable$
  33.  
  34. Set objShell = CreateObject("WScript.Shell")
  35. InternetSetOptionA 0, 39, 0, 0
  36.  
  37. regPath$ = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\"
  38. regServer = regPath$ & "ProxyServer"
  39. regOverride = regPath$ & "ProxyOverride"
  40. regEnable = regPath$ & "ProxyEnable"
  41.  
  42. If bEnable = False Then GoTo Skipper
  43.  
  44. objShell.RegWrite regServer, sProxyServer, "REG_SZ"
  45. objShell.RegWrite regOverride, sProxyOverride & "<local>", "REG_SZ"
  46.  
  47. Skipper:
  48. objShell.RegWrite regEnable, IIf(bEnable, "1", "0"), "REG_DWORD"
  49. MsgBox "Proxy Is " & IIf(bEnable, "Enabled", "Disabled")
  50.  
  51. Shell "C:\Program Files\Internet Explorer\iexplore.exe -nohome", vbNormalFocus
  52. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement