如果排序窗口中没有您想要使用的属性,请使用宏进行排序。
注: 本主题仅适用于已生成到 Microsoft Word 文档中的模板。
使用宏进行排序的优点是:
- 宏可以在模板中的任何表上运行。
- 如果任何表中使用 <RPE_SORT> 备注,那么对该表进行排序。
- 未使用 <RPE_SORT> 备注的表不进行排序。
- 可以从最终输出中删除 <RPE_SORT> 备注。
- 在要排序的列上添加宏标签:
- 在 Document Studio 应用程序中打开模板。
- 在据以排序的列的标题单元格中,添加
备注元素。
- 双击备注元素,然后输入 <RPE_SORT>。
- 对要更新的模板中的每个表重复。
- 创建宏:
- 打开 Microsoft Word。
- 将以下脚本复制并粘贴到文件中:
' Macro: sort tables
' This macro is provided as is with no implicit or explicit support from IBM.
Sub sortTables()
Dim tbl As Table
' sort each table
For Each tbl In ActiveDocument.Tables
' determine if the table has header
Dim hasheader As Boolean
hasheader = False
If tbl.Rows.First.HeadingFormat = True Then
hasheader = True
End If
' get the column to do the sorting on. The column is identified by a comment with the "<RPE_SORT>" content in its first cell
Dim hcell As Cell
Dim index As Integer
pos = 0
For Each hcell In tbl.Rows.First.Cells
hcell.Select
If Selection.Comments.Count > 0 Then
If Selection.Comments.Item(1).Range.Text = "<RPE_SORT>" Then
pos = hcell.ColumnIndex
' Delete the comment - remove comment from the line below
' Selection.Comments.Item(1).Delete
Exit For
End If
End If
Next
' sorts the table using the found column
If pos > 0 Then
Dim fldnum As String
fldnum = "Column " + CStr(pos)
Debug.Print "Sorting on: "; fldnum
tbl.Select
Selection.Sort ExcludeHeader:=hasheader, FieldNumber:=fldnum, SortFieldType:=wdSortFieldAlphanumeric, SortOrder:=wdSortOrderAscending
End If
Next
End Sub
- 以 .dot 扩展名保存文件。
- 将宏添加至文档规范,然后生成输出:
- 打开现有的文档规范,或者在 Launcher 应用程序中创建新的文档规范。
- 展开输出,右键单击目标:Word,然后选择配置输出。
- 对于样式表,请单击浏览以找到您创建的 .dot 宏。
- 对于宏,输入宏的名称。
- 生成输出。
保存宏,并在任何文档规范中复用以便对数据进行排序。