0

I am developing Word web Add-in using OfficeJS, Now I have an accordian, If the user click on accordian menu, I need to make a selection over the header text in Word document, wherever it exists (at least one occurrence).

Is there any way to search the texts and make selection over that in Word doc using OfficeJS.Context ?

Office.context.document.setSelectedDataAsync(WordToSelect, function (asyncResult) {    });

I got the above code, that is inserting the specified text and select instead of searching and do

1
  • Which version of Word are you targeting (your code sample is the 2013 version of the APIS)? What you can do depends on that. What do you mean by "make a selection over the header text"? Do you mean select it, as when a user selects? Or do you mean something else (the term "over" makes me unsure)? Commented Jul 6, 2018 at 14:22

1 Answer 1

1

You need to use the search API on the header for this. Here is a quick sample on how to do it. (assumes "Hello World" is typed on the header :))

async function run() {
    await Word.run(async (context) => {

        let searchResults = context.document.sections.getFirst().getHeader("primary").search("World");
        searchResults.load();

        await context.sync();

        //select the first one found
        searchResults.items[0].select();
    });
}
couple of things to keep in mind when working with headers:

  1. As you probably know, Word documents can have multiple sections and hence multiple sets of headers and footers. So if you want to cover all the cases make sure to traverse the sections collection.
  2. Each section can contain 3 headers/footers, primary, even, first page. would be interesting to see where you will do the search/selection in a situation where you have all options.

hope this sets you up in the right direction.

Sign up to request clarification or add additional context in comments.

1 Comment

I tried this and its working context.document.sections.getFirst().body.search(WordToSelect);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.