I'm creating a Web Addin for word using react and OfficeJs, how do I write a function…
- User
I'm creating a Web Addin for word using react and OfficeJs, how do I write a function that gets the selected text in Word and returns it as a string?
Assistant
Here is how you can get the selected text from Word and return it as a string in a React component using the Office JS API:
import OfficeJS from "office-js"; export default function GetSelectedText() { const getSelectedText = () => { return new Promise((resolve, reject) => { Word.run(context => { const selectedText = context.document.getSelection().text; return context.sync() .then(() => { resolve(selectedText); }) .catch(error => { reject(error); }); }); }); } return getSelectedText(); }The key points:
- Use
Word.runto execute code that interacts with the Word document
- Use
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.