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

时间:2018-01-16 17:57:44

标签: vba api web exchange-server exchangewebservices

我正在尝试使用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是正确的,因为我从EWS的C#运行中检索它: / p>

            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

由于

1 个答案:

答案 0 :(得分:1)

在您使用的FindItem示例中,所有EWS请求都应该是POST(例如,无论您执行什么操作,都要始终发布SOAP XML)

xmlMethod = "GET"

所以这不正确。