Report  zWordChecker.

"-Begin-----------------------------------------------------------------
"-
"- How to use Word spelling or grammar checker with ABAP.
"- The interprocess communication is via Windows clipboard.
"-
"- Author: Stefan Schnell
"- Version: 0.2
"- Date: 09/10/02
"-
"-----------------------------------------------------------------------

  Data Text2Correct(65535Type c Value ''.
  Data CorrectedText(65535Type c Value ''.

  Text2Correct = 'Deis sit ien Tset'.

  Perform WordChecker Using Text2Correct
                      Changing CorrectedText.

  Write CorrectedText.


  "-Begin WordChecker function------------------------------------------
  "-
  "- Argument: Text2Correct
  "-
  "- Return:   CorrectedText
  "-
  "---------------------------------------------------------------------
    Include Ole2Incl.

    Form WordChecker Using Text2Correct Type c
                     Changing CorrectedText Type c.

      "-Constants-------------------------------------------------------
        Constants wdStory Type i Value 6.
        Constants wdMove Type i Value 0.
        Constants wdDoNotSaveChanges Type i Value 0.

      "-Structures------------------------------------------------------
        DataBegin Of lTextClip,
                ClipLine(65535Type c Value '',
              End Of lTextClip.
        Data TextClip Like Standard Table Of lTextClip.

        "-ReturnCode----------------------------------------------------
          Data rc Type i Value 0.

        "-Objects-------------------------------------------------------
          Data oWord Type OLE2_OBJECT.
          Data oDocs Type OLE2_OBJECT.
          Data oDoc Type OLE2_OBJECT.
          Data oSel Type OLE2_OBJECT.

      "-MainForm--------------------------------------------------------
        "-Append the text to correct to the table-----------------------
          Append Text2Correct To TextClip.
        "-Create Word appliction----------------------------------------
          Create Object oWord 'Word.Application'.
          If sy-subrc = 0.
            "-Word application is not visible---------------------------
              Set Property Of oWord 'Visible' = 0.
            "-Create a new document-------------------------------------
              Get Property Of oWord 'Documents' = oDocs.
              Call Method Of oDocs 'Add' = oDoc.
            "-Copy the text to clipboard--------------------------------
              Call Method cl_gui_frontend_services=>clipboard_export
                Importing
                  data = TextClip[]
                Changing
                  rc = rc.
              "-If copy to clipboard is successful----------------------
                If rc = 0.
                  "-Copy text from clipboard to the Word document-------
                    Get Property Of oWord 'Selection' = oSel.
                    Call Method Of oSel 'EndKey'
                      Exporting
                        #1 = wdStory
                        #2 = wdMove.
                    Call Method Of oSel 'Paste'.
                    Call Method Of oSel 'HomeKey'
                      Exporting
                        #1 = wdStory.
                    Call Function 'FLUSH'
                      Exceptions
                        Others = 0.
                  "-Start spelling check--------------------------------
                    Call Method Of oDoc 'CheckSpelling'.
                    "or
                    "Call Method Of oDoc 'CheckGrammar'.
                    Set Property Of oWord 'Visible' = 0.
                    Call Function 'FLUSH'
                      Exceptions
                        Others = 0.
                  "-Copy corrected text from Word document to clipboard-
                    Call Method Of oSel 'WholeStory'.
                    Call Method Of oSel 'Copy'.
                    Call Function 'FLUSH'
                      Exceptions
                        Others = 0.
                  "-Close the Word document and quit the application----
                    Call Method Of oDoc 'Close'
                      Exporting
                        #1 = wdDoNotSaveChanges.
                    Call Method Of oWord 'Quit'.
                  "-Free the selection object---------------------------
                    Free Object oSel.
                  "-Copy text from clipboard----------------------------
                    Call Method cl_gui_frontend_services=>clipboard_import
                      Importing
                        data = TextClip[].
                  "-Get the corrected text------------------------------
                  "-
                  "- Hint: In index 1 you find the original uncorrected
                  "-       text
                  "-
                  "-----------------------------------------------------
                    Read Table TextClip Into lTextClip Index 2.
                    CorrectedText = lTextClip-ClipLine.
                EndIf.
            "-Free the other Word objects-------------------------------
              Free Object oDoc.
              Free Object oDocs.
              Free Object oWord.
          EndIf.

    EndForm.

"-End-------------------------------------------------------------------