This topic describes how to modify the specified text, set the font size of the selected text, and insert a new paragraph at the selected position when you use a text document.
Obtain or set the text in a range
Obtain the text in a specified range.
Syntax
Expression.ActiveDocument.Range(Start, End).TextorExpression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.TextExpression: the document type application object.
Examples
Example 1
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; // Obtain the selected range. const range = await app.ActiveDocument.Range(0, 100); // Obtain the text. const text = await range.Text; console.log(text); // Set the text. range.Text = 'WebOffice'; }Example 2
//@file=base.docx async function example() { await instance.ready(); const app = instance.Application; // Obtain the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Obtain the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Obtain the range object of the cell. const range = await cellOne.Range; // Obtain the text. const text = await range.Text; console.log(text); // Set the text. range.Text = 'WebOffice'; }
Replace specified text
Match and replace the text based on the array that you pass in.
Syntax
Expression.ActiveDocument.ReplaceText(Array.<Object>)Expression: the document type application object.
Parameters
Pass in an array that represents the list of text to be replaced.
Parameter
Type
Required
Description
list
Array.<Object>
Yes
The list of text to be replaced.
Description of the list parameter
Field
Type
Required
Description
key
String
Yes
The old text.
value
String
Yes
The new text.
options
Object
No
The replace options.
Description of the options field
Property
Type
Required
Description
isWildcardMatched
Boolean
No
Specifies whether to use wildcards. The asterisk (*) and question mark (?) wildcard characters are supported.
Asterisk (*): matches any string.
Question mark (?): matches a single character. The matched character must exist.
isCaseSensitive
Boolean
No
Specifies whether the text is case-sensitive.
isWholeWordMatched
Boolean
No
Specifies whether to use whole word match.
isWidthIgnored
Boolean
No
Specifies whether to support both full-width and half-width characters.
Return value
A value of the
booleantype.trueindicates that the replacement is successful.falseindicates that the replacement failed.Example
async function example() { await instance.ready(); const app = instance.Application; const isSuccess = await app.ActiveDocument.ReplaceText([ { key: 'WebOffice', value: 'js-sdk', }, ]); console.log(isSuccess); //true }
Insert the specified text
Insert the specified text after a selection.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.InsertAfter(Text)Expression: the document type application object.
Parameters
Parameter
Type
Required
Description
Text
String
Yes
The text to be inserted.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert the specified text after the selection. await app.ActiveDocument.ActiveWindow.Selection.InsertAfter('Text'); }
Insert a break
Insert a break after a selection.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.InsertBreak()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a break at the end of the selection. await app.ActiveDocument.ActiveWindow.Selection.InsertBreak(); }
Delete a character
Delete the character before the selection.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.TypeBackspace()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Move the cursor down. await app.ActiveDocument.ActiveWindow.Selection.MoveDown(); // Delete the character before the cursor. await app.ActiveDocument.ActiveWindow.Selection.TypeBackspace(); }
Obtain the start position
Obtain the start position of a range.
Syntax
Expression.ActiveDocument.Range(Start, End).StartExpression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.StartExpression: the document type application object.
Examples
Example 1
async function example() { await instance.ready(); const app = instance.Application; // Obtain the selected range. const range = await app.ActiveDocument.Range(0, 100); // Obtain the start position of the selected text. const start = await range.Start; console.log(start); }Example 2
async function example() { await instance.ready(); const app = instance.Application; // Obtain the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Obtain the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Obtain the range object of the cell. const range = await cellOne.Range; // Obtain the start position of the selected cell. const start = await range.Start; console.log(start); }
Obtain the end position
Obtain the end position of a range.
Syntax
Expression.ActiveDocument.Range(Start, End).EndorExpression.ActiveDocument.Tables.Item(Index).Rows.Item(Index).Cells.Item(Index).Range.EndExpression: the document type application object.
Example
Example 1
async function example() { await instance.ready(); const app = instance.Application; // Obtain the selected range. const range = await app.ActiveDocument.Range(0, 100); // Obtain the end position of the selected text. const end = await range.End; console.log(end); }Example 2
async function example() { await instance.ready(); const app = instance.Application; // Obtain the first table. const tableOne = await app.ActiveDocument.Tables.Item(1); // Obtain the first cell in the first row of the table. const cellOne = await tableOne.Rows.Item(1).Cells.Item(1); // Obtain the range object of the cell. const range = await cellOne.Range; // Obtain the end position of the selected cell. const end = await range.End; console.log(end); }
Font
Obtain the font object
Obtain the Font object to set the font of the selected content, such as the font type and font size.
Syntax
Expression.ActiveDocument.Range(Start, End).FontExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; }
Set the font type
Set the font type of the selected text.
Syntax
Expression.ActiveDocument.Range(Start, End).Font.NameExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; // Set the font type of the selected text. font.Name ='SimSun'; }
Set the font size
Set the font size of the selected text.
Syntax
Expression.ActiveDocument.Range(Start, End).Font.SizeExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; // Set the font size of the selected text. font.Size = 30; }
Set the font color
Set the font color of the selected text. You can use one of the following methods to set the font color:
Method 1:
Syntax
Expression.ActiveDocument.Range(Start, End).Font.ColorExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; // Set the font color of the selected text. font.Color = '#228B22'; }
Method 2:
Syntax
Expression.ActiveDocument.Range(Start, End).Font.ColorIndex(WdColorIndex)Expression: the document type application object.
Parameters
Parameter
Type
Required
Description
WdColorIndex
Enum
Yes
The font color. Valid values of
Enum.WdColorIndex:-1 or wdByAuthor: color defined by the document author.
0 or wdAutor: the default color, which is usually black. This is the default value.
1 or wdBlackr: black color.
2 or wdBluer: blue color.
3 or wdTurquoiser: turquoise color.
4 or wdBrightGreen: bright green color.
5 or wdPink: pink color.
6 or wdRed: red color.
7 or wdYellow: yellow color.
8 or wdWhite: white color.
9 or wdDarkBlue: dark blue color.
10 or wdTeal: teal color.
11 or wdGreen: green color.
12 or wdViolet: violet color.
13 or wdDarkRed: dark red color.
14 or wdDarkYellow: dark yellow color.
15 or wdGray50: shade 50 of gray color.
16 or wdGray25: shade 25 of gray color.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; // Set the font color of the selected text. await font.ColorIndex(2); }
Hightlight the text
Syntax
Expression.ActiveDocument.Range(Start, End).Font.HighLightExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the font object. const font = await app.ActiveDocument.Range(0, 20).Font; // Highlight the selected text. font.HighLight = '#228B22'; }
Paragraph
Obtain the paragraph object
Syntax
Expression.ActiveDocument.Range(Start, End).ParagraphFormatExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the paragraph object. const paragraphFormat = await app.ActiveDocument.Range(0, 20).ParagraphFormat; }
Insert a paragraph
Insert a new paragraph at the selection.
Syntax
Expression.ActiveDocument.ActiveWindow.Selection.InsertParagraph()Expression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Insert a new paragraph at the selection. await app.ActiveDocument.ActiveWindow.Selection.InsertParagraph(); }
Set the first-line indent
Syntax
Expression.ActiveDocument.Range(Start, End).ParagraphFormat.CharacterUnitFirstLineIndentExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the paragraph object. const paragraphFormat = await app.ActiveDocument.Range(0, 20).ParagraphFormat; // Indent the first line by 2 units. paragraphFormat.CharacterUnitFirstLineIndent = 2; }
Set the line spacing
Syntax
Expression.ActiveDocument.Range(Start, End).ParagraphFormat.LineSpacingRuleExpression: the document type application object.
Example
async function example() { await instance.ready(); const app = instance.Application; // Obtain the paragraph object. const paragraphFormat = await app.ActiveDocument.Range(0, 20).ParagraphFormat; // Set 1.5 times line spacing for the paragraph. paragraphFormat.LineSpacingRule = 1.5; }