使用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 VBADictionaryorCollectionobject (depending on the JSON structure). This is perfect for reading/modifying JSON data in VBA, but you can't pass this object directly toxhr.Send()—the XMLHTTP request expects a raw text string (or byte array) as the request body, not a parsed object.- Similarly,
Debug.Print convertedJsonfails because VBA doesn't know how to natively print aDictionary/Collectionobject. 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 > Referencesand check the box for VBA-JSON). - Your
Content-Typeheader 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

