对 Microsoft Word 文档使用宏进行排序

如果排序窗口中没有您想要使用的属性,请使用宏进行排序。

开始之前

注: 本主题仅适用于已生成到 Microsoft Word 文档中的模板。

关于此任务

使用宏进行排序的优点是:

过程

  1. 在要排序的列上添加宏标签:
    1. 在 Document Studio 应用程序中打开模板。
    2. 在据以排序的列的标题单元格中,添加 “备注”图标 备注元素。
    3. 双击备注元素,然后输入 <RPE_SORT>
    4. 对要更新的模板中的每个表重复。
  2. 创建宏:
    1. 打开 Microsoft Word。
    2. 将以下脚本复制并粘贴到文件中:
      ' 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. 生成输出。

下一步做什么

保存宏,并在任何文档规范中复用以便对数据进行排序。

反馈