Purpose
We want to call MS Word automation functions from a GuiXT script

Solution

  • Call a JavaScript function via CallJS
  • Get the MS Word automation object in the JavaScript function with guixt.GetObject('Word.Application')
  • Apply the MS Word automation functions to achieve the desired result


Example
In transaction IW32, SAP uses MS Word to maintain the order header text. We would like to give the user the option of inserting predefined text modules into the text at the touch of a button.

The user clicks on the "Insert Safety Text" button, which inserts the predefined "Safety text" at the cursor position:





GuiXT Script "saplstxx.e2102.txt"

GuiXT
// IW32: add standard text buttons
if Q[transaction=IW32]
  
  PushButton (toolbar) "Insert Prio 1 Text" _ 
    process="insert_word_text.txt"
    Using TEXTPART = "prio1"
  
  PushButton (toolbar) "Insert Safety Text" _
    process="insert_word_text.txt"
    Using TEXTPART = "safety"
  
endif

InputScript ""insert_word_text.txt""

GuiXT
Parameter TEXTPART

// Read predefined text
CopyText fromFile="iw\textparts\&U[TEXTPART].txt" toText="mytext"

// Insert text into Word document
CallJSasync word_insert_text mytext 

Return


JavaScript function ""word_insert_text()""

JavaScript
// insert text in Word
function word_insert_text(textname) {

    var word = guixt.GetObject('Word.Application');

    if (!word) {
        return "Word not found";
    };

    var doc = word.ActiveDocument;

    if (!doc) {
        return "No Word document active";
    };

    var text = guixt.getText(textname)

    // insert text at cursor position
    doc.ActiveWindow.Selection.InsertAfter(text);

    return "ok";
}

Debugging tip for JavaScript and the automation functions

  • Use Visual Studio and insert a “debugger” statement into your JavaScript function.
  • Attach Visual Studio to the SAP GUI process (saplogon.exe), the code type is “script code”.

If you now click on the GuiXT button to insert text, the Visual Studio Debugger opens and you have all debugging functions available, including the display of the “Word” object with all its properties and methods:


Components
InputAssistant + Controls