VBA – WinHttpRequest with Login and Password

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on May 3, 2024

This article demonstrates how to use VBA for a WinHttpRequest with Login and Password.


Looking up data on the internet is now pretty much the norm.   Almost every cloud based application has the ability for a user to look up data using an API.   The data can often be extracted into Excel using an HTTP web request and is amazingly simple to do so.   If you require authentication using a user name and password, this is also easy to do so as it just requires one additional line of code in your VBA.

WinHTTP Request – Late Binding

  1. You can declare your WinHttpRequest variable as an object which means that you do not have to add a reference to your VBA project in order for your code to work.
Dim objWinHTTP As Object
Set objWinHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
  1. Once you have declared your object, you can assign the URL you are targeting to a string variable and then send a GET request.
Dim url As String
url = "https://seller-api.xyz.com/v1/sales/"
objWinHTTP.Open "GET", url, False


  1. At this stage, depending on the API that you are targeting, you can set you request header and /or credentials where you would change USERNAME and PASSWORD to the username and password provided.
objWinHTTP.setRequestHeader "Accept", "application/json"
  1. You can then send the request.
  1. A message box can return the response to the request.
WinHTTP MsgBox objWinHTTP.ResponseText
  1. Finally, you can clear the object variable.
Set objWinHTTP = Nothing

The entire code should look like the example below:

vba http late binding example


Request – Early Binding

To use early binding, we need to replace the variable that is set to an object as a WinHttpRequest object.

Dim objWinHTTP As WinHttpRequest
Set objWinHTTP = New WinHttpRequest

In order to do this, you need to add the following reference to your VBA project.

  1. In the VBE, in the menu, select Tools > References.


vba http menu


  1. Select Microsoft WinHTTP Services, version 5.1 and then click OK.


vba http reference


  1. Now, in your code, declare your object as a WinHttpRequest and then set the object as a New WinHttpRequest.

vba http early binding example




Set a reference to Microsoft WinHTTP Services

Replace USERNAME and PASSWORD with your own info.

This returns the XML to a msgbox for demonstration, you can import it a map or load it to a MSXML2.DOMDocument(I’ve got working code, I’m still experimenting, I’ll follow up). Here is a snippet of the msgbox:


VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro – A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text


Learn More!

<<Return to VBA Examples


VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples