none
Office Apps (add ins) using word 2016 (JavaScript) Search Text in word document issue RRS feed

  • Question

  • Hey,

    I am working on Office word Add ins (task pane) using JavaScript (MS Office 2016) (VS 2013). 

    I am following this API => https://msdn.microsoft.com/en-us/library/office/mt598683.aspx

    using {martchCase : false} and also tried put others wordSearch options, but it gives me error.

    Is there any updation in your office.js reference file ? which is not updated in API docs. or its my mistakes.

    Please give me a needful solution for this ASAP, its a life saving for me.

    (my need is to search text on word document and also scroll docs to this text's page).

    Thank you,

    Sohel.

    Wednesday, March 2, 2016 7:51 AM

Answers

  • Hi Sohel,

    There is a spell mistake for the "{martchCase : false}" . The correct search options should be "matchCase:false". And here is an example that search the specific text ignore the case and select the first match word in Word 2016 document:

    function searchText() {
            var text = $("#searchText").val();
            // Run a batch operation against the Word object model.
            Word.run(function (context) {
    
                // Queue a command to search the document based on a prefix.
                var searchResults = context.document.body.search(text, { matchCase: false });
            
    
                context.load(searchResults);
    
                // Synchronize the document state by executing the queued commands, 
                // and return a promise to indicate task completion.
                return context.sync().then(function () {
                    console.log('Found count: ' + searchResults.items.length);
    
                //select the match words
                    if (searchResults.items.length > 0) {
                        searchResults.items[0].select();
                    }
    
               
                    return context.sync();
                });
            })
            .catch(function (error) {
                console.log('Error: ' + JSON.stringify(error));
                if (error instanceof OfficeExtension.Error) {
                    console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                }
            });
        }
    Regards & Fei


    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.

    • Marked as answer by Sohel Vali Tuesday, March 8, 2016 2:14 PM
    Thursday, March 3, 2016 3:09 AM
    Moderator
  • Great !

    Now, works for me, just installed updates in my Office 2016.

    Thanks & Regards.

    Friday, March 4, 2016 10:54 AM

All replies

  • i am using : <script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>
    Wednesday, March 2, 2016 7:54 AM
  • Hi Sohel,

    There is a spell mistake for the "{martchCase : false}" . The correct search options should be "matchCase:false". And here is an example that search the specific text ignore the case and select the first match word in Word 2016 document:

    function searchText() {
            var text = $("#searchText").val();
            // Run a batch operation against the Word object model.
            Word.run(function (context) {
    
                // Queue a command to search the document based on a prefix.
                var searchResults = context.document.body.search(text, { matchCase: false });
            
    
                context.load(searchResults);
    
                // Synchronize the document state by executing the queued commands, 
                // and return a promise to indicate task completion.
                return context.sync().then(function () {
                    console.log('Found count: ' + searchResults.items.length);
    
                //select the match words
                    if (searchResults.items.length > 0) {
                        searchResults.items[0].select();
                    }
    
               
                    return context.sync();
                });
            })
            .catch(function (error) {
                console.log('Error: ' + JSON.stringify(error));
                if (error instanceof OfficeExtension.Error) {
                    console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                }
            });
        }
    Regards & Fei


    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.

    • Marked as answer by Sohel Vali Tuesday, March 8, 2016 2:14 PM
    Thursday, March 3, 2016 3:09 AM
    Moderator
  • still getting same error.
    Thursday, March 3, 2016 9:02 AM
  • you can download my demo project which is also getting same error from :

    https://github.com/denialcakewalk/OfficeAddinsWordSearchText.git

    Thursday, March 3, 2016 9:56 AM
  • demo project download link :

    https://github.com/sohelvali/searchText-OfficeAddins-Word.git

    Thursday, March 3, 2016 10:15 AM
  • Hi Sohel,

    I have down loaded the project and it works well for me. What's the error message did you get?

    It seems that there is something wrong with the Office 2016 on the computer, I suggest that you try to update/repair it to see whether it works well for you.

    And if it still doesn't work please try to test it with a clean machine to see whether it is helpful.

    Regards & Fei


    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.

    Friday, March 4, 2016 5:42 AM
    Moderator
  • Thank you,

    I am using trial version of Office 2016.

    and the error i am getting is :

    {"name":"OfficeExtension.Error","code":"InvalidArgument","message":"InvalidArgument","traceMessages":[],"debugInfo":{"errorLocation":""}}

    Friday, March 4, 2016 7:24 AM
  • InvalidArgument: InvalidArgument
       at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:159239)
       at yi (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:172927)
       at st (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:173014)
       at d (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:172834)
       at c (https://appsforoffice.microsoft.com/lib/1/hosted/word-win32-16.01.js:19:171420)
    Friday, March 4, 2016 10:20 AM
  • also tried on fresh machine and valid license version of MS Office 2016, still getting same above error.

    Friday, March 4, 2016 10:22 AM
  • Great !

    Now, works for me, just installed updates in my Office 2016.

    Thanks & Regards.

    Friday, March 4, 2016 10:54 AM
  • Can you please guide me how i get current active page while i select text from word document to read ?

    Friday, March 4, 2016 2:08 PM
  • Hi Sohel,

    Glad to hear that the issue was resolved. Since the new issue is not relative to the original issue, to get more effective response, I recommend you that open a new question.

    And others who have the same issue would recognize the thread easily and would get benefit from these threads.

    Thanks for your understanding.

    Regards & Fei


    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, March 7, 2016 11:40 AM
    Moderator
  • yeah, i already open new thread for it here it is : 

    https://social.msdn.microsoft.com/Forums/office/en-US/b1e8a226-68ca-4f54-be59-e9ef88c5e6fc/how-to-get-current-active-page-while-i-select-text-from-word-document-to-read-selection?forum=appsforoffice

    Monday, March 7, 2016 12:19 PM