none
Retrieve all rich text fields from document

    Question

  • I'm writing a Word Task Pane app and basically the only way to sync data between app and the Word document is via Rich Text Fields. Now let's say I have 16 fields in total, but I don't know which ones the document contains. Is there any way to just retrieve all rich text fields from a document without knowing their ID / name?

    My current approach is to just call

    Office.context.document.bindings.addFromNamedItemAsync(name, Office.BindingType.Text, {id: name}, function (result) {...}


    after the app has loaded and scan for ALL 16 fields no matter if they exist or not. However that feels not quite right.

    Monday, May 12, 2014 10:51 AM

Answers

  • Hi,

    >>Is there any way to just retrieve all rich text fields from a document without knowing their ID / name? <<

    In JavaScript API for Office, there are some methods of Bindings object as followed.

    We could use Bindings.getAllAsync method to get all bindings that were previously created. If the binding objects in your document are all rich text fields, you could achieve the goal with this method. If not, I’m afraid there is no method to pick out rich text fields from lots of binding objects without ID.

    Here is a sample for your reference.

    function displayAllBindingNames() {
        Office.context.document.bindings.getAllAsync(function (asyncResult) {
            var bindingString = '';
            for (var i in asyncResult.value) {
                bindingString += asyncResult.value[i].id + '\n';
            }
            write('Existing bindings: ' + bindingString);
        });
    }
    // Function that writes to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 13, 2014 5:35 AM
  • Hi,

    >>Unfortunately this only works for bindings that were already created, but that does not help when I open a new document where no bindings exist (but multiple rich text fields). So each rich text field has an ID, but no binding.<<

    Yes, we could only assess to the seleced data and Bindings objects in App for Office currently. If you want to retrieve all rich text fields from a document, you need to create Bindings objects for them.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 19, 2014 9:09 AM

All replies

  • Hi,

    >>Is there any way to just retrieve all rich text fields from a document without knowing their ID / name? <<

    In JavaScript API for Office, there are some methods of Bindings object as followed.

    We could use Bindings.getAllAsync method to get all bindings that were previously created. If the binding objects in your document are all rich text fields, you could achieve the goal with this method. If not, I’m afraid there is no method to pick out rich text fields from lots of binding objects without ID.

    Here is a sample for your reference.

    function displayAllBindingNames() {
        Office.context.document.bindings.getAllAsync(function (asyncResult) {
            var bindingString = '';
            for (var i in asyncResult.value) {
                bindingString += asyncResult.value[i].id + '\n';
            }
            write('Existing bindings: ' + bindingString);
        });
    }
    // Function that writes to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, May 13, 2014 5:35 AM
  • Thanks for the aswer. Essentiually that is the way I do it right now.

    Unfortunately this only works for bindings that were already created, but that does not help when I open a new document where no bindings exist (but multiple rich text fields). So each rich text field has an ID, but no binding. Therefore I now kind of "brute force" through all IDs and create a binding when an ID was found. That is the only "solution" I found so far.
    Friday, May 16, 2014 8:18 AM
  • Hi,

    >>Unfortunately this only works for bindings that were already created, but that does not help when I open a new document where no bindings exist (but multiple rich text fields). So each rich text field has an ID, but no binding.<<

    Yes, we could only assess to the seleced data and Bindings objects in App for Office currently. If you want to retrieve all rich text fields from a document, you need to create Bindings objects for them.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, May 19, 2014 9:09 AM