You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:使用VBA和ADODB无法获取SharePoint列表的Person类型列

SharePoint列表Person类型字段无法通过ADODB获取的问题

问题描述

使用VBA结合ADODB组件读取SharePoint列表数据时,其他类型字段都能正常获取,但Person类型的列始终无法被读取。已确认该列存在于列表中,即使指定字段名而非用*查询,也无法捕获到该字段(通过If fieldName = "ColumnOfTypePersonName"判断从未触发)。

用户代码如下:

Dim Conn As Object
Dim Rec_Set As Object
Dim Sql As String
Dim i As Integer
Dim fieldName As String
Dim fieldValue As Variant

Set Conn = CreateObject("ADODB.Connection")
Set Rec_Set = CreateObject("ADODB.Recordset")
Set resultTable = CreateObject("Scripting.Dictionary")

With Conn
    .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=0;RetrieveIds=Yes;" & _
                        "DATABASE=" & "mySharePointURL;" & _
                        "LIST={listGuid};"
    .Open
End With

Sql = "SELECT * FROM [MyListName];"

With Rec_Set
    If .State = 1 Then .Close
    
    .ActiveConnection = Conn
    .CursorType = adOpenDynamic
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .Source = Sql
    .Open

    If .RecordCount > 0 Then
        For i = 0 To .Fields.Count - 1
            fieldName = .Fields(i).Name
            fieldValue = .Fields(i).Value
            If fieldName = "ColumnOfTypePersonName" Then
                MsgBox fieldValue 'Never hit!
            End If
            resultTable.Add fieldName, fieldValue
        Next i
    End If

    .Close
End With

Set Rec_Set = Nothing

原因分析

  • 字段名映射规则:SharePoint的Person字段通过ACE OLEDB驱动访问时,不会直接返回原字段名,而是拆分为带后缀的子字段,比如[原字段名].Title(用户名)、[原字段名].ID(用户ID)等,直接用原字段名匹配自然无法命中。
  • IMEX模式限制:连接字符串中IMEX=0(编辑模式)下,驱动会将Person字段视为可编辑的对象类型,不会将其作为普通可枚举字段返回。
  • 驱动兼容性:Microsoft.ACE.OLEDB.12.0对SharePoint复杂类型字段的支持有限,不会直接返回完整用户对象,而是拆分出独立的子属性字段。

解决方案

1. 修改IMEX模式为只读

将连接字符串中的IMEX=0改为IMEX=1,让驱动将所有字段视为文本类型读取,更容易捕获Person字段的子属性:

.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;WSS;IMEX=1;RetrieveIds=Yes;" & _
                    "DATABASE=" & "mySharePointURL;" & _
                    "LIST={listGuid};"

2. 匹配带后缀的字段名

遍历字段时,通过包含原字段名的方式匹配,或者先打印所有字段名确认实际返回的名称:

For i = 0 To .Fields.Count - 1
    fieldName = .Fields(i).Name
    Debug.Print fieldName '在VBA立即窗口查看所有返回的字段名
    If InStr(fieldName, "ColumnOfTypePersonName") > 0 Then
        MsgBox .Fields(i).Value '捕获用户名称或ID等属性
    End If
    resultTable.Add fieldName, fieldValue
Next i

3. 直接指定子属性查询

在SQL语句中明确指定要获取的Person字段子属性,比如用户名:

Sql = "SELECT [ColumnOfTypePersonName].Title FROM [MyListName];"

内容的提问来源于stack exchange,提问作者Louitbol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 09:41:15