Get proxy server with VBA without using API calls

Deutz

Board Regular
Joined
Nov 30, 2009
Messages
210
Office Version
  1. 365
Platform
  1. Windows
Hi and thanks in advance,

I have a VBA URL checker in Excel that needs to provide our organisation's proxy server address to the WinHttp.WinHttpRequest.5.1 object.

I used an API to achieve that in the past but am unable to use API calls any longer due to changes to security within our organisation.

I just need some VBA to obtain the address in case it changes over time. Have searched for this code for a while now but can only find API ways to do this.

Kind regards
Deutz
 
You have not provided much information so I am not too sure what you're after.
Your previous code could have been of some help.
BTW, if security settings have been changed you may not be able to obtain this information. But without more knowledge of your system one can only guess.
See if this works for you:
VBA Code:
Option Explicit

Enum CMDModeOptions
    optDirectCMD = 1
    optClipboard = 2
    optTempFile = 3
End Enum

Sub getProxyPowerShell()
    Dim cmd As String
    Dim result As String
    cmd = "(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyServer"
    result = cliCmdExecute(cmd, True, optDirectCMD)
    ActiveCell.Value = result
    ActiveCell.Offset(1).Select
    Debug.Print "proxy: " & Trim(Replace(Replace(result, Chr(10), ""), Chr(13), ""))
    cmd = "(Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').proxyEnable"
    result = cliCmdExecute(cmd, True, optDirectCMD)
    ActiveCell.Value = result
    ActiveCell.Offset(1).Select
    Debug.Print "status: " & Trim(Replace(Replace(result, Chr(10), ""), Chr(13), ""))
End Sub


Function cliCmdExecute(ByVal cli_command As String, Optional usePowerShell As Boolean = False, Optional ByVal Mode As CMDModeOptions = CMDModeOptions.optTempFile) As String
   
    If cli_command = "" Then Exit Function
    Dim shell_prefix As String, shell_suffix As String
    Dim Results As String, objShell As Object
    Dim fullCommand As String
   
    If usePowerShell Then
        shell_prefix = "PowerShell  -ExecutionPolicy Bypass -Outputformat Text -Command """
        shell_suffix = """"
    Else
        shell_prefix = "cmd /C "
        shell_suffix = ""
    End If
   
    fullCommand = shell_prefix & cli_command
   
    Set objShell = CreateObject("Wscript.Shell")
   
    Select Case Mode
        Case optDirectCMD
            Results = objShell.Exec(fullCommand).StdOut.ReadAll()
       
        Case optClipboard
            fullCommand = fullCommand & " | clip" & shell_suffix
            objShell.Run fullCommand, 0, True
            'Stop
            On Error Resume Next
            With CreateObject("htmlfile")
                Results = .ParentWindow.ClipboardData.GetData("text")
                .ParentWindow.ClipboardData.clearData ("text")
            End With
            On Error Resume Next
        Case optTempFile
            Dim tmpfile As String: tmpfile = generateTempFileName
'            fullCommand = fullCommand & " > """ & tmpfile & "" & shell_suffix
            fullCommand = fullCommand & " *> " & tmpfile & shell_suffix
            objShell.Run fullCommand, 0, True
           
            On Error Resume Next
            With CreateObject("Scripting.FileSystemObject")
                If .GetFile(tmpfile).Size > 0 Then
                    Results = .OpenTextFile(tmpfile).ReadAll()
                End If
                .DeleteFile tmpfile
            End With
            On Error GoTo 0
        Case Else
            Exit Function
    End Select
    If Len(Results) = 0 Then Exit Function
   
    cliCmdExecute = Results
    'Debug.Print "CMD results ready:", fullCommand

    Set objShell = Nothing
End Function

Function generateTempFileName(Optional str1 As String, Optional strSeparator As String = "_") As String
    On Error Resume Next
    str1 = Trim(str1)
    If Len(str1) = 0 Then str1 = "tmpOut"
    generateTempFileName = TrailingSlash(Environ("temp")) & _
                    str1 & strSeparator & _
                    Format(Now, "yyyymmdd" & strSeparator & "hhnnss") & strSeparator & _
                    Int(Rnd * 100000) & ".txt"
End Function

Public Function TrailingSlash(varIn As Variant) As String
'The TrailingSlash() function just ensures that the folder names we are processing _
end with the slash character.
    If Len(varIn) > 0& Then
        If Right(varIn, 1&) = Application.PathSeparator Then TrailingSlash = varIn Else TrailingSlash = varIn & Application.PathSeparator
    End If
End Function
If it works at all some things can be fine-tuned later.
 
Upvote 0
I don't know why I took the long and winding road, but try this as well:
VBA Code:
Sub getProxyRegRead()
    Dim regKey As String
    Dim result As String
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyServer"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "proxy: " & result
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyEnable"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "status: " & result
End Sub
Results show up in the Immediate window of VBE
 
Upvote 0
I don't know why I took the long and winding road, but try this as well:
VBA Code:
Sub getProxyRegRead()
    Dim regKey As String
    Dim result As String
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyServer"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "proxy: " & result
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyEnable"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "status: " & result
End Sub
Results show up in the Immediate window of VBE
Hi bobsan42,

Thanks for your code. I just tried this smaller sub and I get an error, unable to open the registry key for reading on this line:
VBA Code:
result = CreateObject("WScript.Shell").RegRead(regKey)
 
Upvote 0
hello
maybe read the proxyenable key before ;)

VBA Code:
Sub getProxyRegRead()
    Dim regKey As String
    Dim result As String
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyEnable"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "status: " & result
    If result <> 0 Then
        regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyServer"
        result = CreateObject("WScript.Shell").RegRead(regKey)
        Debug.Print "proxy: " & result
    Else
        Debug.Print "proxy not working"
    End If
End Sub
 
Upvote 0
hello
maybe read the proxyenable key before ;)

VBA Code:
Sub getProxyRegRead()
    Dim regKey As String
    Dim result As String
    regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyEnable"
    result = CreateObject("WScript.Shell").RegRead(regKey)
    Debug.Print "status: " & result
    If result <> 0 Then
        regKey = "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\proxyServer"
        result = CreateObject("WScript.Shell").RegRead(regKey)
        Debug.Print "proxy: " & result
    Else
        Debug.Print "proxy not working"
    End If
End Sub
Hey Patrick,

Thanks for your reply. When I run this sub i get from Debug.print ...

status: 0
proxy not working

Is proving to be more difficult than I thought it would be.
 
Upvote 0
re
Well, that means your proxy isn't configured correctly and isn't working properly
and/or the proxyserver key doesn't exist. Just explore the directory tree for that key in REGEDIT and you'll see for yourself.
 
Upvote 0
re
Well, that means your proxy isn't configured correctly and isn't working properly
and/or the proxyserver key doesn't exist. Just explore the directory tree for that key in REGEDIT and you'll see for yourself.
Ok, thanks for your efforts, much appreciated.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top