如果您想使用的內容在排序視窗中無法使用,請使用巨集來排序。
開始之前
註: 這個主題僅適用於產生成 Microsoft Word 文件的範本。
關於這項作業
使用巨集來排序的好處如下:
- 可以在範本中的任何表格上執行巨集。
- 只要表格中有使用 <RPE_SORT> 註解,就會排序該表格。
- 沒有使用 <RPE_SORT> 註解的表格就不會排序。
- <RPE_SORT> 註解可以從最終輸出中刪除。
程序
- 在要排序的直欄中新增巨集標籤:
- 在 Document Studio 應用程式中開啟範本。
- 在要排序的直欄的標頭資料格中,新增
「註解」元素。
- 按兩下「註解」元素,並輸入 <RPE_SORT>。
- 針對範本中的每一個表格重複上述動作,以便更新。
- 建立巨集:
- 開啟 Microsoft Word。
- 複製下列 Script,並貼到檔案中:
' 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 巨集。
- 針對巨集,輸入巨集的名稱。
- 產生輸出。
下一步
儲存巨集,並在任何文件規格中重複使用它來排序資料。