我正在尝试通过VBA将列表从SharePoint导入到Excel。我确实知道服务器的名称,但是我不确定如何找出LISTNAME
和VIEWNAME
变量,我也想自动使用默认(Windows)凭据登录到SharePoint。我可以将其插入代码中吗?
这是我的代码(出于安全原因,我不得不使用XXXX清除一些条目),我很感谢您的帮助:
Sub ImportSPList()
Dim objMyList As ListObject
Dim objWksheet As Worksheet
Dim strSPServer As String
Const SERVER As String = "https://xxxxxx.xxx.xxxx.net/sites/RiskMgmt/xxxAudit"
Const LISTNAME As String = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}"
Const VIEWNAME As String = "ALL Datasheet View"
strSPServer = SERVER
Set objWksheet = Worksheets.Add
Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, _
Array(strSPServer, LISTNAME, VIEWNAME), False, , Range("A1"))
End Sub
答案 0 :(得分:1)
尝试下面的示例代码,您将需要添加更多字段以在此处阅读
代码
Option Explicit
' === SharePoint Site and List Settings ===
Const SERVERUrl As String = "https://xxxxxx.xxx.xxxx.net/sites/RiskMgmt/xxxAudit/"
Const ListName As String = "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx}"
'Const VIEWNAME As String = "ALL Datasheet View" ' <-- Currently not used, using the ListName
' === Parameters for using ADO with Late Binding ===
Const adOpenDynamic = 2
Const adOpenStatic = 3
Const adUseClient = 3
Const adUseNone = 1
Const adUseServer = 2
Const adLockPessimistic = 2
Const adLockOptimistic = 3
Const adLockBatchOptimistic = 4
Const adAddNew = &H1000400
Const adUpdate = &H1008000
Const adSearchForward = 1
' === Field Names in SharePoint List ===
Const ProjectNum As String = "Project Number"
' Add more fields here
' =======================================================================
Sub ImportSPList()
Dim Conn As Object
Dim Rec_Set As Object
Dim Sql As String
Dim objWksheet As Worksheet
Set objWksheet = Worksheets.Add
On Error GoTo ErrHand
' Create the connection object with ADO
Set Conn = CreateObject("ADODB.Connection")
Set Rec_Set = CreateObject("ADODB.Recordset")
' Open the connection and submit the update
With Conn
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
"DATABASE=" & SERVERUrl & ";" & _
"LIST=" & ListName & ";"
.Open
End With
' add a Query to select all records from your List
Sql = "SELECT * FROM [MyName_List] ;" ' <--- CHANGE TO YOUR LIST NAME
With Rec_Set
' confirm that recordset is closed
If .State = 1 Then .Close
' Recordset settings parameters
.ActiveConnection = Conn
.CursorType = adOpenDynamic ' adOpenStatic,
.CursorLocation = adUseClient
.LockType = adLockOptimistic ' adLockPessimistic
.Source = Sql
.Open
' check how many # of records returned
If .RecordCount < 1 Then ' no records
' do someting >> maybe MSGBOX
Else ' at least 1 record found >> read the row's data
Do While Not .EOF
objWksheet.Range("A2").Value = .Fields(ProjectNum).Value ' read the value of field "Project Number" from list to cell
' add more fields below
.MoveNext
Loop
End If
.Close
End With
Set Rec_Set = Nothing
CleanExit:
If Conn.State = 1 Then
Conn.Close
Set Conn = Nothing
End If
MsgBox "Finished reading data from SharePoint list", vbOKOnly
ErrHand:
Debug.Print Err.Number, Err.Description
End Sub
答案 1 :(得分:0)
我设法以这种方式修复它,结果是服务器地址错误,但是"VIEWNAME"
也可以留为空白:
Sub SharePoint_Import()
Dim objMyList As ListObject
Dim objWksheet As Worksheet
Dim strSPServer As String
Dim RData As Worksheet
Const SERVER As String = "xxxx.xxxxx.xxx.net/xxxx/xxxx" 'SP server
Const LISTNAME As String = "{1234567-1234-1234-1234-1234567891}" 'SP List ID
Const VIEWNAME As String = ""
Set RData = Sheets("rawdata") 'reset import sheet
RData.UsedRange.ClearContents
strSPServer = "https://" & SERVER & "/_vti_bin" '<- _vti_bin is necessary
Set objWksheet = RData
Set objMyList = objWksheet.ListObjects.Add(xlSrcExternal, Array(strSPServer, LISTNAME, VIEWNAME), False, , Range("A1"))
Set objMyList = Nothing
Set objWksheet = Nothing
End Sub