使用したいプロパティーがソート・ウィンドウでは選択できない場合は、マクロを使用してソートします。
始める前に
注: このトピックは、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 拡張子を付けて保存します。
- 以下のようにしてマクロを文書仕様に追加し、出力を生成します。
- 文書仕様を、既にそれがある場合には開き、ない場合にはランチャー・アプリケーションで作成します。
- 「出力」を展開し、「ターゲット: Word」を右クリックし、「出力の構成」を選択します。
- 「スタイル・シート」で、「参照」をクリックして、作成した .dot マクロを見つけます。
- 「マクロ」には、マクロの名前を入力します。
- 出力を生成します。
次のタスク
マクロを保存して、データをソートするために任意の文書仕様で再利用します。