2017-01-07 177 views
0

使用下面的代碼導入使用Excel VBA從SharePoint數據,以下錯誤出現:如何將SharePoint列表導入Excel VBA?

運行時錯誤「1004」:
發生意外錯誤。更改您的數據無法保存

Sub ImportSharePointList() 

    Dim objMyList As ListObject 
    Dim objWksheet As Worksheet 
    Dim strSPServer As String 


    Const SERVER As String = "<server>:<Port>/Location/Lists" 
    Const LISTNAME As String = "{4e78c371-9ff0-410d-86a5-b78499091369}" 
    Const VIEWNAME As String = "{C3BD0F82-6BCD-45EF-B43E-DCFEA78067B0}" 

    strSPServer = "http://" & SERVER & "/_vti_bin" 

    Set objWksheet = Worksheets.Add 

    Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, _ 
     Array(strSPServer, LISTNAME, VIEWNAME), False, xlYes, Range("A1")) 

End Sub 
+0

從「所有網站內容」的URL獲取服務器字符串修復了問題。 – Chakri

回答

0

像這樣的東西應該爲你工作。

Sub ImportSharePointList() 

    Dim objMyList As ListObject 
    Dim objWksheet As Worksheet 
    Dim strSPServer As String 
    Const SERVER As String = "mysite:8003/sites/the-sharepoint-thing/" 
    Const LISTNAME As String = "{XXXXXXXXXXXX}" 
    Const VIEWNAME As String = "{XXXXXXXXXXXX}" 

    ' The SharePoint server URL pointing to 
    ' the SharePoint list to import into Excel. 
    strSPServer = "http://" & SERVER & "/_vti_bin" 
    ' Add a new worksheet to the active workbook. 
    Set objWksheet = Worksheets.Add 
    ' Add a list range to the newly created worksheet 
    ' and populated it with the data from the SharePoint list. 
    Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, _ 
     Array(strSPServer, LISTNAME, VIEWNAME), False, , Range("A1")) 

End Sub