【问题标题】:Importing SharePoint List to Excel via VBA通过 VBA 将 SharePoint 列表导入 Excel
【发布时间】:2025-12-03 18:55:01
【问题描述】:

我正在尝试通过 VBA 将列表从 SharePoint 导入 Excel。我确实知道服务器名称,但我不确定如何找出 LISTNAMEVIEWNAME 变量,我还想使用默认 (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

【问题讨论】:

  • 这取决于您希望如何将数据带入。我使用"ADODB.Connection""ADODB.Recordset" 连接到SharePoint 中的列表,然后从Excel 中读取(或更新它们)值。
  • 这可能吗?我使用 ADODB 连接进行 SQL 查询到 excel,我不知道如何为 SharePoint 设置它。你能发布一个示例代码吗?

标签: excel vba sharepoint


【解决方案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

【讨论】:

    【解决方案2】:

    我设法通过这种方式修复它,结果服务器地址错误,但"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
    

    【讨论】:

    • 嗨@Rhyfelwr 我可以知道从哪里获取 SharePoint 中的列表名和视图名吗?我是否需要拥有管理员权限或其他权限才能访问这些信息?