主题
文字组件选区操作相关 API
获取选区对象
ActiveDocument.ActiveWindow.Selection
表示窗口或窗格中的当前选定内容。
选定内容表示文档中的选定(或突出显示)区域,或者代表插入点(如果未选择文档中的任何内容)。
每个文档窗格只能有一个 Selection 对象,并且在整个应用程序中只能有一个活动的 Selection 对象。
语法
表达式.ActiveDocument.ActiveWindow.Selection
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 选区对象
  const selection = await app.ActiveDocument.ActiveWindow.Selection
}
删除内容
ActiveDocument.ActiveWindow.Selection.Delete()
删除选区内容
语法
表达式.ActiveDocument.ActiveWindow.Selection.Delete(WdUnits)
表达式:文档类型应用对象
参数
WdUnits 是枚举,具体可看: Enum.WdUnits
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 删除选区内容
  app.ActiveDocument.ActiveWindow.Selection.Delete(1)
}
选区向下移动
ActiveDocument.ActiveWindow.Selection.MoveDown()
将选区范围向下移动
语法
表达式.ActiveDocument.ActiveWindow.Selection.MoveDown()
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 将光标向下移动
  await app.ActiveDocument.ActiveWindow.Selection.MoveDown()
}
选区向上移动
ActiveDocument.ActiveWindow.Selection.MoveUp()
将选区范围向上移动
语法
表达式.ActiveDocument.ActiveWindow.Selection.MoveUp()
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 将光标向上移动
  await app.ActiveDocument.ActiveWindow.Selection.MoveUp()
}
选区向左移动
ActiveDocument.ActiveWindow.Selection.MoveLeft()
将选区范围向左移动
语法
表达式.ActiveDocument.ActiveWindow.Selection.MoveLeft()
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 将光标向左移动
  await app.ActiveDocument.ActiveWindow.Selection.MoveLeft()
}
选区向右移动
ActiveDocument.ActiveWindow.Selection.MoveRight()
将选区范围向右移动
语法
表达式.ActiveDocument.ActiveWindow.Selection.MoveRight()
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 将光标向右移动
  await app.ActiveDocument.ActiveWindow.Selection.MoveRight()
}
选区区域
ActiveDocument.ActiveWindow.Selection.Range
返回一个 Range 对象, 该对象代表指定对象中包含的文档部分。
语法
表达式.ActiveDocument.ActiveWindow.Selection.Range
表达式:文档类型应用对象
示例
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 选区对象
  const selection = await app.ActiveDocument.ActiveWindow.Selection
  // 区域对象
  const range = await selection.Range
}
修改范围
ActiveDocument.Range(Start, End).SetRange()
修改区域范围
语法
表达式.ActiveDocument.Range(Start, End).SetRange({ Start, End })
或者 表达式.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.SetRange({ Start, End })
表达式:文档类型应用对象
参数
| 属性 | 类型 | 默认值 | 必填 | 说明 | 
|---|---|---|---|---|
| Start | number | 是 | 区域开始位置 | |
| End | number | 是 | 区域结束位置 | 
示例 1
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 获取选中区域
  const range = await app.ActiveDocument.Range(0, 10)
  // 设置区域范围
  await range.SetRange(10, 20)
}
示例 2
//@file=base.docx
async function example() {
  await instance.ready()
  const app = instance.Application
  // 获取第 1 个表格
  const tableOne = await app.ActiveDocument.Tables.Item(1)
  // 获取表格第 1 行的第 1 个单元格
  const cellOne = await tableOne.Rows.Item(1).Cells.Item(1)
  // 获取该单元格的区域对象
  const range = await cellOne.Range
  // 单元格字体属性
  const font = await range.Font
  // 修改区域范围
  const newRange = await range.SetRange({
    Start: 1,
    End: 10
  })
  const newText = await newRange.Text // 获取新区域文本
  console.log(newText)
}
 金山文档开放平台
金山文档开放平台