一般信息。:我需要從Excel更新(添加/編輯)SharePoint列表。我可以用ListObject
做到這一點,但這不是我們所通向的方向。使用Excel-VBA(MSXML2.XMLHTTP對象)更新SharePoint列表
讀完所有Google的可能性後,我想出了(也許我錯了),想用MSXML2.XMLHTTP
對象來更新SharePoint列表。
當前問題:我的代碼運行一直到結束,我得到objXMLHTTP.Status = 200
,但SharePoint列表未更新。
代碼
Option Explicit
Const SharepointUrl As String = "http://share.corning.com/sites/ipp/PMOSandbox/"
Const ListName As String = "{60CE6622-D25B-447A-BFBF-8F3DD5B9FCF0}"
Const VIEWNAME As String = "{91ADBAE5-479F-4C80-A5FF-8EDA7A233B82}"
Sub Add_Item()
Dim objXMLHTTP As MSXML2.XMLHTTP
Dim strListNameOrGuid As String
Dim strBatchXml As String
Dim strSoapBody As String
Dim ValueVar As String, FieldNameVar As String
Set objXMLHTTP = New MSXML2.XMLHTTP
FieldNameVar = "IPP #"
ValueVar = "Shai"
'Add New Item'
strBatchXml = "<Batch OnError='Continue'><Method ID='1' Cmd='New'><Field Name='" + FieldNameVar + "'>1004</Field>" + _
"<Field Name='Title'>Uploaded from VBA</Field>" + _
"<Field Name='Next KD Status'>" + 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>" & ListName _
& "</listName><updates>" & strBatchXml & "</updates></UpdateListItems></soap:Body></soap:Envelope>"
objXMLHTTP.send strSoapBody
Do
' wait for response
Loop Until objXMLHTTP.Status = 200
Set objXMLHTTP = Nothing
MsgBox "Finished Running !"
End Sub
也許使用ADO會是更好的選擇嗎?你可以用SQL語句更新表。 –
@RyanWildry我很樂意提出建議,你如何實現它? –
與ADO連接,請參閱:https://www.connectionstrings.com/sharepoint/ Microsoft ACE OLEDB 12.0部分,然後使用連接字符串更新表。看起來你已經有了GUID列表,你需要它來設置連接。 –