从VBA发送JSON数据时出现问题

时间:2019-12-17 13:45:59

标签: json excel vba

我正在尝试创建一个Excel宏,以使用POST请求将单元格数据发送到URL。我选择使用JSON格式化数据,因为要发送的数据很多。这是我发送JSON的代码:

    Dim jFullDictionary As New Dictionary ' This dictionary contains a Collection
    ...
    Dim JsonURL As String
    JsonURL = "http://myurl.com"
    Dim JsonHTTP As New MSXML2.XMLHTTP
    Set JsonHTTP = CreateObject("MSXML2.XMLHTTP")
    Dim JsonString As String
    JsonString = JsonConverter.ConvertToJson(jFullDictionary, Whitespace:=3)
    With JsonHTTP
        .Open "POST", JsonURL, False
        .setRequestHeader "Content-type", "application/json"
        .setRequestHeader "Accept", "application/json"
        .setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
        .send JsonString ' This is where the program breaks
    End With

每次运行此命令时,都会收到错误消息(类似)

Run time error: Could not find the resource specified

我已经看过这些StackOverflow线程,但是却一无所获: Error from VB excel macro code - msxml3.dll -2146697211 The system cannot locate the resource specified

Sending JSON POST request in VBA

另外,我尝试从here安装Tim Hall的项目,但是安装在我的机器上不起作用(可能是因为它是一台工作计算机)。

该错误使我认为它与我已加载的资源有关,但我不确定。如果不是那样,那么我的代码有问题。任何帮助将不胜感激,谢谢。

编辑: 这是我用数据填充jFullDictionary的循环:

    Dim jDictionary As New Dictionary
    Dim jCollection As New Collection
    Dim jFullDictionary As New Dictionary

    For i = 1 To excelRange.Rows.Count
        For j = 1 To excelRange.Columns.Count
            jDictionary("row") = i
            jDictionary("name") = Cells(5, j) ' This assignment is arbitrary
            jDictionary("value") = Cells(i + 1, j) ' so is this
            jCollection.Add jDictionary
            Set jDictionary = Nothing
        Next j
    Next i

    jFullDictionary.Add "parametersList", jCollection

0 个答案:

没有答案