Author Topic: Liquid UI - Using Word Automation to Update a Template  (Read 5053 times)

chirag.amin@guixt.com

  • GuiXT Forum
  • Newbie
  • *
  • Posts: 34
    • View Profile
Liquid UI - Using Word Automation to Update a Template
« on: February 03, 2017, 03:53:50 PM »
This forum article will show an example of how we can update a Word document in LiquidUI. We will take data from a notification in IW22 and update a document marked with specific tags. Then the document will be saved as a new file, therefore never overwriting the template document. In order for this to work, you must load wsoffice.dll in the ESESSIONS.SJS file.



LiquidUI Code:

SAPLIQS0.E7200.sjs

// Define an array of tags for the Notification screen
NotificationTags = ["[LIQUID~UI~DESC]","[LIQUID~UI~NOTIFNUM]","[LIQUID~UI~NOTIFTYPE]"];

// Pushbutton that will trigger our function
pushbutton([TOOLBAR],"Update Word Template", "?", {"process":updateDoc, "using":{"tags":NotificationTags}});

// This function will create a Word document
function updateDoc(param){
   
   var tagsArr = param.tags;
   
   // Refer to https://msdn.microsoft.com/en-us/library/office/ff193977.aspx for which parameters Execute takes
   
   // WdFindWrap constants
   wdFindAsk = 2;
   wdFindContinue = 1;
   wdFindStop = 0;
   
   // WdReplace constants
   wdReplaceNone = 0;
   wdReplaceOne = 1;
   wdReplaceAll = 2;

   onscreen 'SAPLIQS0.7200'
      // Create MS Object for Word
      var wordObj = new ActiveXObject('word.application');
      // Do not show the Microsoft Word window
      wordObj.Visible = false;
      doc = wordObj.Documents.Open("C:\\GuiXT\\Tutorials\\Forum\\DocTemplate.docx");
      
      for(i=0; i<tagsArr.length; i++){
         switch(tagsArr){
            case "[LIQUID~UI~DESC]":
               copytext({"fromscreen":"X[TEXT]", "totext":"z_text"});
               // Call a function to hanlde the word update
               updateTemplate(z_text, tagsArr, wordObj);   
               break;
            case "[LIQUID~UI~NOTIFNUM]":
               set("V[z_notifNum]", "&F[Notification]");
               // Call a function to hanlde the word update
               updateTemplate(z_notifNum, tagsArr, wordObj);
               break;
            case "[LIQUID~UI~NOTIFTYPE]":
               set("V[z_notifType]", "&F[RIWO00-QMARTE]");   // Techname for Notification Type
               // Call a function to hanlde the word update
               updateTemplate(z_notifType, tagsArr, wordObj);
               break;
         }
      }
      
      // Save the document as a new file, therefore not getting rid of the Template
      doc.SaveAs("C:\\GuiXT\\Tutorials\\Forum\\"+z_notifNum+"_"+_user+"_Doc.docx");
      wordObj.Quit();
      
      enter("?");
}

// Recursive function to split the string and then update the Word Document
function updateTemplate(str, tag, wordObj){
   // Base case is when string's length is less than 256
   if(str.length < 256){
      // Update the word document and save
      findObj = wordObj.Selection.Find;
      findObj.Execute(tag,true,false,false,false,false,true,wdFindContinue,false,str,wdReplaceOne,false,false,false,false);
   }else{
      // Split the string and add the tag
      str1 = str.substring(0,255-tag.length) + tag;
      str2 = str.substring(255-tag.length,str.length);
      // Update the word document
      findObj = wordObj.Selection.Find;
      findObj.Execute(tag,true,false,false,false,false,true,wdFindContinue,false,str1,wdReplaceOne,false,false,false,false);
      // Call this function again with the remaining characters in the string
      updateTemplate(str2, tag, wordObj);
   }
}


ESESSION.SJS
load("wsoffice");
« Last Edit: February 06, 2017, 11:34:07 AM by chirag.amin@guixt.com »