Search code examples
javascriptangulartypescriptms-wordadd-in

to create dropdown in word add-in using typescript


I want to create a drop-down in word add-in using angular typescript having options yes and no and on the selection of particular option. I want to add a paragraph in a word document. Can any one suggest a good way.


Solution

  • first, create a dropdown in HTML

    <div>
    <select class="form-control" (change)="yesClicked($event.target.value)">
        <option value="choose" >choose option</option>
        <option value="Yes">Yes</option>
        <option value="No" >No</option>
        </select>
    

    then add type scripting code

       yesClicked(eventName:any){
        if(eventName=='Yes')
            {
                this.wordDocument.yesclicked();
            }
            else if(eventName =='No')
                {
    
                    this.wordDocument.Noclicked();
                }
    }
    

    then define the function

        yesclicked()
        {   
         Word.run(function (context) {
         var body = context.document.body;
         body.insertParagraph('Content of a new paragraph', 
         Word.InsertLocation.end);
          return context.sync().then(function () {
        console.log('Paragraph added at the end of the document body.');
          });  
         })   
         .catch(function (error) {
       console.log('Error: ' + JSON.stringify(error));
      if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
       }
     });
    
     }