使用 Microsoft Word 文件的巨集來排序

如果您想使用的內容在排序視窗中無法使用,請使用巨集來排序。

開始之前

註: 這個主題僅適用於產生成 Microsoft Word 文件的範本。

關於這項作業

使用巨集來排序的好處如下:

程序

  1. 在要排序的直欄中新增巨集標籤:
    1. 在 Document Studio 應用程式中開啟範本。
    2. 在要排序的直欄的標頭資料格中,新增 「註解」圖示「註解」元素。
    3. 按兩下「註解」元素,並輸入 <RPE_SORT>
    4. 針對範本中的每一個表格重複上述動作,以便更新。
  2. 建立巨集:
    1. 開啟 Microsoft Word。
    2. 複製下列 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
    3. .dot 副檔名儲存檔案。
  3. 將巨集新增至文件規格,並產生輸出:
    1. 若有文件規格,請開啟它,或是在 Launcher 應用程式中建立文件規格。
    2. 展開輸出,用滑鼠右鍵按一下目標:Word,並選取配置輸出
    3. 針對樣式表,請按一下瀏覽,並找出您所建立的 .dot 巨集。
    4. 針對巨集,輸入巨集的名稱。
    5. 產生輸出。

下一步

儲存巨集,並在任何文件規格中重複使用它來排序資料。

意見