0
我一直在嘗試使用VBA將數據添加到Excel中的Sharepoint 2013列表中,但沒有成功。我正在使用遍佈互聯網的示例。使用VBA將數據添加到Excel中的Sharepoint 2013列表
Sub Add_Item(ListName As String, SharepointUrl As String, ValueVar As String, FieldNameVar As String)
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim strListNameOrGuid As String
Dim strBatchXml As String
Dim strSoapBody As String
Set objXMLHTTP = New MSXML2.XMLHTTP
strListNameOrGuid = ListName
'Add New Item'
strBatchXml = "<Batch OnError='Continue'><Method ID='3' Cmd='New'><Field Name='ID'>New</Field><Field Name=" + FieldNameVar + ">" + ValueVar + "</Field></Method></Batch>"
objXMLHTTP.Open "POST", SharepointUrl + "_vti_bin/Lists.asmx", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
objXMLHTTP.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"
strSoapBody = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " _
& "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " _
& "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><UpdateListItems " _
& "xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>" & strListNameOrGuid _
& "</listName><updates>" & strBatchXml & "</updates></UpdateListItems></soap:Body></soap:Envelope>"
objXMLHTTP.send strSoapBody
If objXMLHTTP.Status = 200 Then
' Do something with response
End If
Set objXMLHTTP = Nothing
End Sub
因爲我是新來的使用XML和SOAP,我決定向後工作,只是嘗試將一個項目添加到列表中。我沒有從表格中獲取數據的問題。我不會將任何參數傳遞給sub,我只是分配值。
我沒有收到任何錯誤。但是沒有數據被寫入到列表中。我甚至得到200的狀態 - 這僅僅意味着「確定」。 我甚至試圖從列表名稱或URL中取出一些字符,看看是否會給出錯誤,但我仍然得到200的狀態。
如果我使用Msxml2.ServerXMLHTTP而不是Msxml2.XMLHTTP我得到狀態爲401.有什麼辦法可以強制執行錯誤,以便我可以看到我需要更正以使其正常工作嗎?
以下是我與合作(希望我刪除任何敏感信息):
Sub Add_Item()
' Hard coding the parameters to start so just testing the output to SharePoint
Dim ListName As String
Dim SharepointUrl As String
Dim Branch_Cost_Center As String
Dim Field1NameVar
ListName = "Nightly Device Tracking Excel XML Test"
SharepointUrl = "https://{myurl}"
Field1NameVar = "Branch Cost Center"
Branch_Cost_Center = "919191"
' Remove the above when passing parameters
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim strListNameOrGuid As String
Dim strBatchXml As String
Dim strSoapBody As String
Set objXMLHTTP = New MSXML2.XMLHTTP
strListNameOrGuid = ListName
'Add New Item'
strBatchXml = "<Batch OnError='Continue'><Method ID='3' Cmd='New'><Field Name='ID'>New</Field><Field Name=" + Field1NameVar + ">" + Branch_Cost_Center + "</Field></Method></Batch>"
objXMLHTTP.Open "POST", SharepointUrl + "_vti_bin/Lists.asmx", False
objXMLHTTP.setRequestHeader "Content-Type", "text/xml; charset=""UTF-8"""
objXMLHTTP.setRequestHeader "SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"
strSoapBody = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " _
& "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " _
& "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><UpdateListItems " _
& "xmlns='http://schemas.microsoft.com/sharepoint/soap/'><listName>" & strListNameOrGuid _
& "</listName><updates>" & strBatchXml & "</updates></UpdateListItems></soap:Body></soap:Envelope>"
'for debugging
Debug.Print strSoapBody
objXMLHTTP.send strSoapBody
If objXMLHTTP.Status = 200 Then
' Do something with response
End If
Set objXMLHTTP = Nothing
End Sub
我一直在使用的內部名稱爲外地和GUID,而不是在結果沒有差異列表名稱嘗試。
我在objXMLHTTP上設置了一個監視變量,但不知道要查找什麼 - 有很多返回的信息。