OfficeDataSourceObject 对象

优质
小牛编辑
129浏览
2023-12-01

表示邮件合并操作中的邮件合并数据源。

使用 OfficeDataSourceObject 对象

要使用 OfficeDataSourceObject 对象,请声明一个 OfficeDataSourceObject 对象的变量。然后即可使用该对象相关的不同属性和方法。使用 SetSortOrder 方法指定如何排序数据源中的记录。本示例首先对数据源按邮政编码降序排序,然后再按姓名升序排序。

Sub SetDataSortOrder()
    Dim appOffice As OfficeDataSourceObject

    Set appOffice = Application.OfficeDataSourceObject
    appOffice.Open bstrConnect:="DRIVER=SQL Server;SERVER=ServerName;" & _
        "UID=user;PWD=;DATABASE=Northwind", bstrTable:="Employees"

    appOffice.SetSortOrder SortField1:="ZipCode", _
        SortAscending1:=False, SortField2:="LastName", _
        SortField3:="FirstName"
End Sub

使用Column,ComparisonCompareToConjunction 属性返回或设置数据源查询条件。本示例更改已有筛选,从邮件合并中删除所有 Region 域不等于 "WA" 的记录。

Sub SetQueryCriterion()
    Dim appOffice As Office.OfficeDataSourceObject
    Dim intItem As Integer

    appOffice.Open bstrConnect:="DRIVER=SQL Server;SERVER=ServerName;" & _
        "UID=user;PWD=;DATABASE=Northwind", bstrTable:="Employees"

    With appOffice.Filters
        For intItem = 1 To .Count
            With .Item(intItem)
                If .Column = "Region" Then
                    .Comparison = msoFilterComparisonNotEqual
                    .CompareTo = "WA"
                    If .Conjunction = "Or" Then .Conjunction = "And"
                End If
            End With
        Next intItem
    End With
End Sub
OfficeDataSourceObject
多个对象