使用 VBA 连接到 EWS - 通过 xml 使用 GET 调用检索数据

Connecting to EWS using VBA - Retrieve data using GET call via xml

我正在尝试使用 VBA 连接到 EWS 服务 - 我认为唯一的方法是使用来自 EWS 的 POST 和 GET API 调用。

此代码非常适合 POST 情况,即发送电子邮件:

            Sub SendMessage()
               Dim sReq As String
               Dim xmlMethod As String
               Dim XMLreq As New MSXML2.XMLHTTP60
               Dim EWSEndPoint As String
               EWSEndPoint = "server_address"
               sReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
               sReq = sReq & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">" & vbCrLf
               sReq = sReq & "<soap:Header>" & vbCrLf
               sReq = sReq & "<t:RequestServerVersion Version=""Exchange2010""/>" & vbCrLf
               sReq = sReq & "</soap:Header>" & vbCrLf
               sReq = sReq & "<soap:Body>" & vbCrLf
               sReq = sReq & "<CreateItem MessageDisposition=""SendAndSaveCopy"" xmlns=""http://schemas.microsoft.com/exchange/services/2006/messages"">" & vbCrLf
               sReq = sReq & "<SavedItemFolderId>" & vbCrLf
               sReq = sReq & "<t:DistinguishedFolderId Id=""sentitems"" />" & vbCrLf
               sReq = sReq & "</SavedItemFolderId>" & vbCrLf
               sReq = sReq & "<Items>" & vbCrLf
               sReq = sReq & "<t:Message>" & vbCrLf
               sReq = sReq & "<t:ItemClass>IPM.Note</t:ItemClass>" & vbCrLf
               sReq = sReq & "<t:Subject>" & "123" & "</t:Subject>" & vbCrLf
               sReq = sReq & "<t:Body BodyType=""Text"">" & "123" & "</t:Body>" & vbCrLf
               sReq = sReq & "<t:ToRecipients>" & vbCrLf
               sReq = sReq & "<t:Mailbox>" & vbCrLf
               sReq = sReq & "<t:EmailAddress>" & "EMAIL" & "</t:EmailAddress>" & vbCrLf
               sReq = sReq & "</t:Mailbox>" & vbCrLf
               sReq = sReq & "</t:ToRecipients>" & vbCrLf
               sReq = sReq & "</t:Message>" & vbCrLf
               sReq = sReq & "</Items>" & vbCrLf
               sReq = sReq & "</CreateItem>" & vbCrLf
               sReq = sReq & "</soap:Body>" & vbCrLf
               sReq = sReq & "</soap:Envelope>" & vbCrLf

               xmlMethod = "POST"
               XMLreq.Open xmlMethod, EWSEndPoint, False, "user","password" 
               XMLreq.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""

               XMLreq.Send sReq

               If XMLreq.Status = 200 Then
                    ' Message Sent okay
                Else
                    ' Something went Wrong
               End If


            End Sub

但是对于接收,它没有给出错误,但似乎没有检索任何数据,这是我的脚本,我确信我使用的文件夹 parentID 是正确的,因为我从 C# 检索它 运行 的 EWS:

            Sub getMessage()
               Dim sReq As String
               Dim xmlMethod As String
               Dim XMLreq As New MSXML2.XMLHTTP60
               Dim EWSEndPoint As String
               Dim xdoc As New MSXML2.DOMDocument
               EWSEndPoint = "serveraddress"

               xdoc.Load ("filepath")

               sReq = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCrLf
               sReq = sReq & "<soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:t=""http://schemas.microsoft.com/exchange/services/2006/types"">" & vbCrLf
               sReq = sReq & "<soap:Header>" & vbCrLf
               sReq = sReq & "<t:RequestServerVersion Version=""Exchange2010_SP1"" />" & vbCrLf
               sReq = sReq & "</soap:Header>" & vbCrLf
               sReq = sReq & "<soap:Body>" & vbCrLf
               sReq = sReq & "<m:FindItem Traversal=""Shallow"">" & vbCrLf
               sReq = sReq & "<m:ItemShape>" & vbCrLf
               sReq = sReq & "<t:BaseShape>IdOnly</t:BaseShape>" & vbCrLf
               sReq = sReq & "<t:AdditionalProperties>" & vbCrLf
               sReq = sReq & "<t:FieldURI FieldURI=""item:Subject"" />" & vbCrLf
               sReq = sReq & "</t:AdditionalProperties>" & vbCrLf
               sReq = sReq & "</m:ItemShape>" & vbCrLf
               sReq = sReq & "<m:ParentFolderIds>" & vbCrLf
               sReq = sReq & " <t:FolderId Id=""parentID"" ChangeKey=""AQAAAA=="" />" & vbCrLf
               sReq = sReq & "</m:ParentFolderIds>" & vbCrLf
               sReq = sReq & "</m:FindItem>" & vbCrLf
               sReq = sReq & "</soap:Body>" & vbCrLf
               sReq = sReq & "</soap:Envelope>" & vbCrLf

               xmlMethod = "GET"
               XMLreq.Open xmlMethod, EWSEndPoint, False, "user", "pass"

               XMLreq.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
               XMLreq.Send sReq

               If XMLreq.Status = 200 Then
                    ' Message Sent okay
                Else
                    ' Something went Wrong
               End If


               VBA_to_append_existing_text_file (XMLreq.responseText)


            End Sub

谢谢

在您使用的 FindItem 示例中,所有 EWS 请求都应 POST(例如,无论您执行何种操作,您总是 POST 访问 SOAP XML)

xmlMethod = "GET"

所以这是不正确的。