Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word Addin/OfficeJS - Detect if Cursor is on Chart Element

I want to be able to check if the document cursor is inside of a Chart element within the MS Word API. Right now I have an application that inserts text, but when I try to insert said text into the title of the Chart, it deletes the chart and replaces it with the Content Control I'm inserting.

Instead of deleting the chart, I want to check if the cursor is inside of the Chart via context. If I'm inside of the Chart in any way, be able to throw a warning message to the user and escape. Is there a way to do this?

like image 203
Judson James Avatar asked Jan 30 '26 10:01

Judson James


1 Answers

This should do it. Like @CindyMeister said you can check the ooxml and inspect it, range.getOoxml() will only return something if the cursor is on some xml object that's been inserted into the document.

example.ts

Word.run(async (context: RequestContext) => {
  const range: Range = context.document.getSelection();
  const ooxml: ClientResult<string> = range.getOoxml();
  context.load(range);
  await context.sync();

  const ooxmlVal = ooxml.value;
  if (ooxmlVal) {

    const lowered = ooxmlVal.toLowerCase();
    const isChart = lowered.includes("excel") && lowered.includes("chart");
    if (isChart) {
      console.log("CURSOR IS ON CHART");
    }
  }
});
like image 167
DjH Avatar answered Feb 02 '26 02:02

DjH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!