0

enter image description herethe error is shown in the attached image

hello every one please help me out with this i want to redirect the image when clicking on image id in the same document

  Office.context.document.addHandlerAsync(
    Office.EventType.DocumentSelectionChanged,
    (result:any) => {
      let bookmark = localStorage.getItem("bookmark")
      let bookmarkParse = JSON.parse(bookmark);
      console.log("bookmarkParse",bookmarkParse)
      bookmarkParse.map((item: any) => {
      Word.run(async (context) => {
        // var doc:any = context.document;
        console.log("result", result);
        const range :any = context.document.getSelection();            
        return context.sync().then(async function (){
          context.load(range);
          await context.sync();
          let text = range.text
          console.log("item", item.ImageId, text);      
          if (item.ImageId == text) {
            console.log("item bookmark",item.bookmark)               
            // range.hyperlink = "#" + item.bookmark;
            context.document.getSelection().hyperlink = `"#" + ${item.bookmark}`;
            return context.sync();
          } else {
            console.log("range not matched");
          }              
          await context.sync();
        });           
      })
    })
    })

  • The `map` function is a loop. You should not have calls of `context.sync` in a loop. See [Avoid context.sync in loops](https://learn.microsoft.com/office/dev/add-ins/concepts/correlated-objects-pattern). Also, your code is hard to read because you use a mixture of TypeScript constructions (like datatype labels) and old-fashioned JavaScript. You use the `async` keyword, but you use `then` instead of `await`. Finally, you need to explain what is the content of the `bookmark` variable. It is JSON apparently, but what exactly is in it? – Rick Kirkham Dec 07 '22 at 23:30
  • @RickKirkham Kirkham its actually kind type id of image within the bookmark something like that "Bookmark1670503859095" – Vikash Choudhary Dec 08 '22 at 12:51

0 Answers0