Search code examples
vbaexcelcloudant

Connect to Cloudant via Excel Macro


I want to connect to a Cloudant database from an Excel Macro that I have written. The macro would essentially need to find data in a specific field searching on another field. Example: For ID="2", return data in field "Name". Does anyone know how this is possible?

Thanks.

Edit: I have posted an answer to my own question. The answer I posted gets all docs from a specified database. From here you can query, etc. to get the specific data you are looking for. You can also use an excel macro JSON parser, found here to help sort through the data. The Base64Encoder I used can be found here.


Solution

  • In case anyone else ever searches for this, I figured I would upload an actual response to this question (nearly a month later) rather than just a "yes, this is possible".

    Since Cloudant requires Basic Auth, the way that I have found to do this is below:

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    Dim response As String
        'Sameple URL: https://ibmcds.cloudant.com/sandbox/_all_docs
    URL = "https://" + CloudantUsername + ".cloudant.com/" + DatabaseName + "/_all_docs"
    With objHTTP
        .Open "GET", URL, False
        .SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        .SetRequestHeader "Content-Type", "application/json"
        .SetRequestHeader "Accept", "application/json"
        .SetRequestHeader "Authorization", "Basic " + Base64Encode(CloudantUsername + ":" + CloudantPassword)
        .Send ("")
    End With
    response = objHTTP.responseText
    

    In this example, the Base64Encode function just encodes the given string. You can use any Base64 Encoder for this.

    And if you want to check the status of the request you can use:

    If objHTTP.Status = 200 Then
        response = objHTTP.responseText
        MsgBox "This request is valid.", vbOKOnly
    Else
        MsgBox "This request is not valid.", vbOKOnly
    End If
    

    Or something similar.

    I hope this helps anyone else who may be looking to do something like this.