JavaScript - Snippets
Inserted Formatted Text
$("#add-text").click(() => tryCatch(addFormattedText));
$("#add-paragraph").click(() => tryCatch(addFormattedParagraph));
$("#add-preset").click(() => tryCatch(addPreStyledFormattedText));
async function addFormattedText() {
await Word.run(async (context) => {
let sentence = context.document.body.insertText("This is some formatted text!", "Replace");
sentence.font.set({
name: "Courier New",
bold: true,
size: 18
});
await context.sync();
});
}
async function addFormattedParagraph() {
await Word.run(async (context) => {
let secondSentence = context.document.body.insertParagraph("This is the first text with a custom style.", "End");
secondSentence.font.set({
bold: false,
italic: true,
name: "Berlin Sans FB",
color: "blue",
size: 30
});
await context.sync();
});
}
async function addPreStyledFormattedText() {
await Word.run(async (context) => {
let sentence = context.document.body.insertParagraph("To be or not to be", "End");
sentence.styleBuiltIn = Word.Style.intenseReference;
await context.sync();
});
}
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
console.error(error);
}
}
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrev