Range
APIs for working with the Range object in Word documents.
Range
ActiveDocument.Range(Start, End)
Returns a Range object defined by a start and end character position.
This feature is supported in JS-SDK V1.1.10 and later.
Syntax
-
expression.ActiveDocument.Range(Start, End)Or
expression.ActiveDocument.ActiveWindow.Selection.RangeOr
expression.ActiveDocument.Tables.Item(Index).RangeOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Rangeexpression: A document application object.
-
Examples
-
Get a Range object from the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); } -
Get a Range object from a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; }
-
Methods
ActiveDocument.Range().Item()
Returns a specified range within a Range object.
-
Syntax
expression.ActiveDocument.Range(Start, End).Item({ Start, End })Or
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Item({ Start, End })Expression: Application object of the document type
-
Parameters
Property
Data type
Required
Description
Start
Number
Yes
The start position.
End
Number
Yes
The end position.
-
Examples
-
Get a specified range from the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the specified range. const item = await range.Item(0, 50); } -
Get a specified range from a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the specified range. const item = await range.Item(1, 10); }
-
ActiveDocument.Range(Start, End).GetHtmlData()
Returns formatted HTML data from a specified range.
This feature is supported in JS-SDK V1.1.14 and later.
-
Syntax
expression.ActiveDocument.Range(Start, End).GetHtmlData()expression: A document application object.
-
Return value
Property
Data type
Description
HTML
String
The HTML data.
Text
String
The text data.
-
Example
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(10, 20); // Get the formatted HTML data of the specified range. const htmlInfo = await range.GetHtmlData(); console.log(htmlInfo); }
ActiveDocument.Range(Start, End).PasteHtml()
Pastes formatted HTML data into a specified range.
This feature is supported in JS-SDK V1.1.14 and later.
-
Syntax
expression.ActiveDocument.Range(Start, End).PasteHtml({ HTML })expression: A document application object.
-
Parameters
Property
Data type
Required
Description
HTML
String
Yes
The HTML data to paste. Use the
ActiveDocument.Range(Start, End).GetHtmlData()method to get this data. -
Example
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(10, 20); // Get the formatted HTML data of the specified range. const htmlInfo = await range.GetHtmlData(); // Paste the formatted HTML data to the specified range. await app.ActiveDocument.Range(110, 110).PasteHtml({ HTML: htmlInfo.HTML, }); }
ActiveDocument.Range(Start, End).SetRange()
Sets the start and end character positions of an existing range.
-
Syntax
expression.ActiveDocument.Range(Start, End).SetRange({ Start, End })Or
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.SetRange({ Start, End })expression: A document application object.
-
Parameters
Property
Data type
Required
Description
Start
Number
Yes
The start position of the range.
End
Number
Yes
The end position of the range.
-
Examples
-
Set the start and end positions of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Set the start and end character positions of the range. await range.SetRange(10, 20); } -
Set the start and end positions of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Set the start and end character positions of the range. const newRange = await range.SetRange({ Start: 1, End: 10, }); }
-
ActiveDocument.Range(Start, End).Find
Finds content within a specified range.
This feature is supported in JS-SDK V1.1.19 and later.
-
Syntax
expression.ActiveDocument.Range(Start, End).Findexpression: A document application object.
-
Parameters
Property
Data type
Default
Required
Description
Text
String
Yes
The text to search for.
ShowHighlight
Boolean
true
No
Whether to highlight the search result.
FilterConfig
Object
true
No
Search filter configuration. Supported in WebOffice v8.5.1 and later.
-
FilterConfig parameters
Property
Data type
Default
Required
Description
isCaseSensitive
Boolean
false
No
Whether the search is case-sensitive.
isWholeWordMatched
Boolean
false
No
Whether to match whole words only.
isWidthIgnored
Boolean
false
No
Whether to ignore character width (full-width and half-width).
isWildcardMatched
Boolean
false
No
Whether to use wildcard characters.
isRegexMatched
Boolean
false
No
Whether to use regular expressions. If both wildcard characters and regular expressions are enabled, regular expressions take precedence.
-
Example
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = await app.ActiveDocument.Range(0, 10); // Find the content. range.Find('WPS') }
Properties
ActiveDocument.Range(Start, End).Start
Returns the start character position of a range.
-
Syntax
expression.ActiveDocument.Range(Start, End).StartOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Startexpression: A document application object.
-
Examples
-
Get the start position of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the start position. const start = await range.Start; console.log(start); } -
Get the start position of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the start position. const start = await range.Start; console.log(start); }
-
ActiveDocument.Range(Start, End).End
Returns the end character position of a range.
-
Syntax
expression.ActiveDocument.Range(Start, End).EndOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Endexpression: A document application object.
-
Examples
-
Get the end position of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the end position. const end = await range.End; console.log(end); } -
Get the end position of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the end position. const end = await range.End; console.log(end); }
-
ActiveDocument.Range(Start, End).Font
Returns the Font object of a specified range.
-
Syntax
expression.ActiveDocument.Range(Start, End).FontOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Fontexpression: A document application object.
-
Examples
-
Get the Font object of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the Font object. const font = await range.Font; console.log(font); } -
Get the Font object of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the Font object. const font = await range.Font; }
-
ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndex
Sets or returns the persistent highlight color of a range. The highlight persists after a page refresh.
-
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndex = WdColorIndexexpression: A document application object.
-
Parameters
Property
Data type
Required
Description
WdColorIndex
Enum
No
The highlight color of the range. For more information, see WdColorIndex.
-
Example
async function example() { await instance.ready(); const app = instance.Application; // Set the highlight color for the range. The highlight persists after a page refresh. app.ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndex = 7; }
ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndexTemp
Sets or returns the temporary highlight color of a range. The highlight disappears after a page refresh.
-
Syntax
expression.ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndexTemp = WdColorIndexexpression: A document application object.
-
Parameters
Property
Data type
Required
Description
WdColorIndex
Enum
No
The highlight color of the range. For more information, see WdColorIndex.
-
Example
async function example() { await instance.ready(); const app = instance.Application; // Set the temporary highlight color for the range. The highlight disappears after a page refresh. app.ActiveDocument.ActiveWindow.Selection.Range.HighlightColorIndexTemp = 7; }
ActiveDocument.Range.Information()
Returns page information for a range. Currently, only the total page count is supported.
This feature is supported in JS-SDK V1.1.10 and later.
Because documents use a streaming layout, the total page count is determined only after you scroll to the end of the document.
-
Syntax
expression.ActiveDocument.Range.Information(WdInformation)expression: A document application object.
-
Parameters
Use
app.Enum.WdInformationto specify the type of information to retrieve.Property
Data type
Required
Description
WdInformation
Enum
Yes
The page information to retrieve. Currently, only the total page count is supported. For more information, see WdInformation.
-
Return value
Property
Data type
Description
PagesCount
Number
The number of pages that have been typeset.
End
Boolean
Whether typesetting is complete.
-
Example
async function example() { await instance.ready(); const app = instance.Application; // Get the total number of pages. const totalPages = await app.ActiveDocument.Range.Information(app.Enum.WdInformation.wdNumberOfPagesInDocument); console.log(totalPages); }
ActiveDocument.Range(Start, End).Paragraphs
Returns all paragraph objects within a specified range.
This feature is supported in JS-SDK V1.1.14 and later.
-
Syntax
expression.ActiveDocument.Range(Start, End).ParagraphsOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Paragraphsexpression: A document application object.
-
Examples
-
Get all paragraph objects in a range from the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get all paragraph objects. const info = await range.Paragraphs; } -
Get all paragraph objects in a range from a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get all paragraph objects. const paragraphFormat = await range.Paragraphs; }
-
ActiveDocument.Range(Start, End).ParagraphFormat
Returns the paragraph format of a specified range.
-
Syntax
expression.ActiveDocument.Range(Start, End).ParagraphFormatOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.ParagraphFormatexpression: A document application object.
-
Examples
-
Get the paragraph format of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the paragraph format. const info = await range.ParagraphFormat; } -
Get the paragraph format of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the paragraph format. const paragraphFormat = await range.ParagraphFormat; }
-
ActiveDocument.Range(Start, End).Text
Gets or sets the text content of a range.
Getting text is supported in JS-SDK V1.1.10 and later.
Setting text is supported in JS-SDK V1.1.11 and later.
-
Syntax
expression.ActiveDocument.Range(Start, End).TextOr
expression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.Textexpression: A document application object.
-
Examples
-
Get and set the text of a range in the document:
async function example() { await instance.ready(); const app = instance.Application; // Get the Range object. const range = app.ActiveDocument.Range(0, 100); // Get the text. const text = await range.Text; console.log(text); // Set the text. range.Text = 'Aliyun'; } -
Get and set the text of a range in a cell:
async function example() { await instance.ready(); const app = instance.Application; // Get the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Get the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Get the Range object of the cell. const range = await cellOne.Range; // Get the text. const text = await range.Text; console.log(text); // Set the text. range.Text = 'Aliyun'; }
-