You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用VBA在Outlook中发送JSON对象失败,无法输出对象求解决

Ah, I see the issue here—you're mixing up parsed JSON objects with raw JSON strings, which is a common gotcha when working with the VBA-JSON converter! Let's break down why this isn't working and how to fix it.

Why Your Current Code Fails

  • JsonConverter.ParseJson() takes a JSON string and converts it into a VBA Dictionary or Collection object (depending on the JSON structure). This is perfect for reading/modifying JSON data in VBA, but you can't pass this object directly to xhr.Send()—the XMLHTTP request expects a raw text string (or byte array) as the request body, not a parsed object.
  • Similarly, Debug.Print convertedJson fails because VBA doesn't know how to natively print a Dictionary/Collection object. You need to convert it back to a readable string first.

Fixes to Get It Working

You have two straightforward options depending on whether you need to modify the JSON data before sending:

Option 1: Send Raw JSON Directly (No Modifications Needed)

If you don't need to edit the JSON content, skip parsing entirely and send the original string:

xhr.Send "{""fields"": 123}"

Option 2: Parse, Modify, Then Convert Back to JSON String

If you need to work with the JSON object in VBA first (e.g., add/change fields), use JsonConverter.ConvertToJson() to turn the parsed object back into a string before sending:

Dim convertedJson As Object
Set convertedJson = JsonConverter.ParseJson("{""fields"": 123}")

' Optional: Modify the JSON object here (example)
convertedJson("fields") = 456
convertedJson("additionalField") = "Outlook Meeting Trigger"

' Convert the object back to a JSON string
Dim jsonPayload As String
jsonPayload = JsonConverter.ConvertToJson(convertedJson)

' Send the string (not the object)
xhr.Send jsonPayload

' Now you can debug the payload
Debug.Print jsonPayload

Full Modified Code Example

Here's how your complete code would look with Option 2 (including minor best practices):

Dim Msg As Outlook.MeetingItem
Set Msg = Item
Set recips = Msg.Recipients
Dim regEx As New RegExp
regEx.Pattern = "^\w+\s\w+,\sI351$"
Dim URL As String
URL = "https://webhook.site/55759d1a-7892-4c20-8d15-3b8b7f1bf3b3"

For Each recip In recips
    If regEx.Test(recip.AddressEntry) And recip.AddressEntry <> "Application Management Linux1, I351" Then
        Dim convertedJson As Object
        Set convertedJson = JsonConverter.ParseJson("{""fields"": 123}")
        
        ' Optional: Make changes to the JSON structure here
        ' convertedJson("meetingSubject") = Msg.Subject
        
        ' Convert parsed object back to JSON string
        Dim jsonPayload As String
        jsonPayload = JsonConverter.ConvertToJson(convertedJson)
        
        Set xhr = CreateObject("MSXML2.ServerXMLHTTP.6.0")
        xhr.Open "POST", URL, False
        xhr.setRequestHeader "Content-Type", "application/json"
        xhr.Send jsonPayload ' Send the string, not the object
        
        ' Debug the sent payload
        Debug.Print jsonPayload
    End If
Next

Quick Notes

  • Ensure you have the VBA-JSON library properly referenced in your Outlook VBA project (go to Tools > References and check the box for VBA-JSON).
  • Your Content-Type header is already set correctly to "application/json"—great job on that!
  • If you run into issues with ConvertToJson, double-check that your parsed object is a valid structure (Dictionary for JSON objects, Collection for JSON arrays) that the converter can serialize.

内容的提问来源于stack exchange,提问作者khashashin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:00:47