This topic describes how to perform common manipulation operations in Word documents.
Allow users to modify content only at the specified position
To allow users to modify content only at the specified position of a Word document, perform the following steps:
Create a
.docx
document. Only this file type supports content controls.Write content to the document.
Insert a content control at the desired position.
Choose Review > Restrict Editing.
Query the position of text and replace the text
Query the position of the text
Use the Range method to obtain the full range of the document. You can also specify a starting position and an ending position to narrow down the search range.
Use JavaScript to find the text and obtain the starting position. For example, you can use the
find
method.
Replace the text
Use the ReplaceText method to replace the text.
Move the cursor over the specified text
Locate the text. Example:
const info = await app.ActiveDocument.Find.Execute('WebOffice'); // Response: [{ pos: 16, len: 2 }]
.Obtain the position information. Example:
const pos = info[0].pos;
.Move the cursor over the text. Example:
await app.ActiveDocument.Range.SetRange(pos, pos);
async function example() {
await instance.ready();
const app = instance.Application;
// 1. Search for and highlight the text.
const findResult = await app.ActiveDocument.Find.Execute('WebOffice');
// 2. Obtain the position information.
const { pos } = findResult[0];
// 3. Obtain the range.
const range = await app.ActiveDocument.Range.SetRange(pos, pos);
}
async function example() {
// Refer to the lines for the previous steps. Code is omitted here.
// 4. Scroll through the document window to display the obtained range in the document window.
await app.ActiveDocument.ActiveWindow.ScrollIntoView(range);
}
Merge content from one document into another document
Document merging is a common document manipulation scenario.
To merge content from Document A into Document B, use ActiveDocument.Range(Start, End).GetHtmlData()
to obtain HTML data of Document A. Sample code:
async function example() {
await instance.ready();
const app = instance.Application;
// Return the specified Range object.
const range = await app.ActiveDocument.Range(10, 20);
// Obtain formatted HTML data of the range.
const htmlInfo = await range.GetHtmlData();
console.log(htmlInfo);
}
The value of the htmlInfo variable is { HTML, Text }
.
Attribute | Type | Description |
HTML | String | HTML data |
Text | String | Text data |
Obtain HTML data and use ActiveDocument.Range(Start, End).PasteHtml({ HTML })
to paste the data into Document B. Sample code:
async function example() {
await instance.ready();
const app = instance.Application;
// Return the specified Range object.
const range = await app.ActiveDocument.Range(10, 20);
// Obtain formatted HTML data of the range.
const htmlInfo = await range.GetHtmlData();
// Paste the formatted HTML data into the specified range.
await app.ActiveDocument.Range(110, 110).PasteHtml({
HTML: htmlInfo.HTML,
});
}
To locate the end of Document B, you can use ActiveDocument.GetDocumentRange()
to obtain the range of the full document and use Range.SetRange()
to set the starting and ending positions. Sample code:
The GetHtmlData
operation requires clipboard capabilities. Make sure that the document allows copy operations. To allow copy operations on a document, set user_acl.copy
for the file/info
operation to 1
.
async function example() {
await instance.ready();
const app = instance.Application;
// Return the specified Range object.
const DocumentRange = await app.ActiveDocument.GetDocumentRange();
// Obtain the end.
const End = await DocumentRange.End;
// Go to the end.
await app.ActiveDocument.Range(0, 1).SetRange({
Start: End,
End: End,
});
}
This way, the content of Document A is appended to the end of Document B. You can also use this solution to merge documents on the frontend.