Trying to find VBA Code to call locally stored X509Certificate2 in to a Module

mwdbActuary

New Member
Joined
May 1, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I am trying to find the means of importing a locally stored self-signed Digital Certificate in to a module. This is needed for a login process to an external website after providing my username and password pair. I have the process working in VB.NET but want to transition to VBA. This is where I am currently with the code:

VBA Code:
Sub Login()
    Dim postData As String
    Dim cert As Object
    Dim request As Object
    Dim dataStream As Object
    Dim writer As Object
    Dim stream As Object
    Dim reader As Object
    Dim loginResponse As Object

    On Error Resume Next

    postData = "username=MY_USERNAME&password=MY_PASSWORD"

    Set cert = CreateObject("X509Certificate2")
    cert.Import "C:\client-2048.p12", ""

    Set request = CreateObject("MSXML2.ServerXMLHTTP.6.0")
    request.Open "POST", "WEBSITE", False
    request.setRequestHeader "X-Application", "MY_APP_KEY"
    request.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
    request.setRequestHeader "Accept", "application/json"
    request.setClientCertificate cert
    request.send postData

    If request.Status = 200 Then.............................

The Locals window would suggest that the Certificate isnt being called and the login is therefore failing. Any help or advice would be greatly appreciated after 4 days of banging my head ;(
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,223,162
Messages
6,170,431
Members
452,326
Latest member
johnshaji

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