Pass session between WinHttp and VBA QueryTables

mongolianboss

New Member
Joined
Aug 31, 2020
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Hello

I have code that uses WinHttp for login. It redirects and needs session key and cookies to authenticate so I can't use QueryTables for authentication.
I also have legacy code that uses QueryTables for pulling data from the same service.

Is it possible to somehow use the same session I'm creating in WinHttp to pull data using QueryTables? At the moment QueryTables won't be authenticated because the session is not shared.
I know can build parser after using WinHttp but it would be much cleaner and I could reuse the legacy code with QueryTables.

My code below:

VBA Code:
Sub Log()


UrlToPostTo = "https://www.test.com"
    Set http = New WinHttp.WinHttpRequest
    http.Open "GET", UrlToPostTo, False
    http.Option(WinHttpRequestOption_EnableRedirects) = True
    http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    http.SetRequestHeader "Connection", "keep-alive"
    http.Send
    If http.Status = "200" Then
        ' parse redirect url
        response = CStr(http.ResponseText)
        searchaction = InStr(1, response, "action", 0) + 8
        searchmethod = InStr(1, response, "method", 0) - 2
        response = Mid(response, searchaction, searchmethod - searchaction)
        response = Replace(response, "amp;", "")
        UrlRedirectedTo = response
        'create POST request
        loginData = "username=user&password=pass"
        http.Open "POST", UrlRedirectedTo, False
        http.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
        http.SetRequestHeader "Connection", "keep-alive"
        http.Send loginData
        ' logged in to www.test.com
    End If



With Sheets("Log").QueryTables.Add(Connection:="https://www.test.com/service1/table1", Destination:=Sheets("Log").Range("$A$1"))
        .Name = "Log"
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlOverwriteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .WebSelectionType = xlSpecifiedTables
        .WebFormatting = xlWebFormattingNone
        .WebPreFormattedTextToColumns = True
        .WebConsecutiveDelimitersAsOne = True
        .WebSingleBlockTextImport = False
        .WebDisableDateRecognition = False
        .WebDisableRedirections = False
        .Refresh BackgroundQuery:=False
        
        
End With
Application.DisplayAlerts = False
Application.DisplayAlerts = True
    
End Sub
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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