代码之家  ›  专栏  ›  技术社区  ›  user8608110

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

  •  0
  • user8608110  · 技术社区  · 7 年前

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

    此代码适用于POST case,即发送电子邮件:

                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#运行中检索到它:

                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 回复  |  直到 7 年前
        1
  •  1
  •   Glen Scales    7 年前

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

    xmlMethod = "GET"
    

    所以这是不对的。