Quantcast
Channel: SCN : All Content - Scripting Languages
Viewing all articles
Browse latest Browse all 522

Tip: Write each table with SAP GUI Scripting to a CSV file

$
0
0

Hello community,
here is a SAP GUI Script to write each table in a file in CSV (Comma Separated Value) format. Use only one sub call

ReadTableInFile "SFLIGHT", "C:\\Dummy\\SFlight.csv"

in your SAP GUI script and you get the table SFLIGHT in the file C:\Dummy\SFlight.csv.
I think it is a good way, if you need to download data tables e.g. to analyze data constellations. So you can download a few data tables without any manual activities.

 

Here you find a description how you can manipulate very big data files and the aspect to connect them to a Microsoft Access database for a fast and flexible

analysis - it is in German language.

 

The following source code is full commented, so I hope it is easy to unterstand how it works.

Any comments are welcome.

 

Cheers
Stefan

'-Begin-----------------------------------------------------------------
'-
'- Author: Stefan Schnell
'- Page: www.stschnell.de
'- Date: 2012/03/31
'-
'-----------------------------------------------------------------------

  '-ReadTableInFile-----------------------------------------------------
    Sub ReadTableInFile(TableName, FileName)

      '-Reset the session-----------------------------------------------
        session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
        session.findById("wnd[0]/tbar[0]/btn[0]").press

      '-Open TAC SE16---------------------------------------------------
        session.findById("wnd[0]/tbar[0]/okcd").text = "/nSE16"
        session.findById("wnd[0]/tbar[0]/btn[0]").press

      '-View table------------------------------------------------------
        session.findById("wnd[0]/usr/ctxtDATABROWSE-TABLENAME").text = _
          TableName
        session.findById("wnd[0]/tbar[1]/btn[7]").press
        session.findById("wnd[0]/tbar[1]/btn[8]").press

      '-Set display to ALV Grid view------------------------------------   
        '-Open user specific parameters dialog--------------------------
        '-
        '- Attention: Here is a language specific code, customize it
        '-
        '---------------------------------------------------------------
          Set Menu = session.findById("wnd[0]/mbar")
          Set Einstellungen = Menu.FindByName("Einstellungen", "GuiMenu")
          Set BenutzerPar = Einstellungen.FindByName("Benutzerparameter...", _
            "GuiMenu")
          BenutzerPar.Select()

        '-Set the display-----------------------------------------------
          Set ALVGridView = session.findById("wnd[1]/usr/tabsG_TABSTRIP/" & _
            "tabp0400/ssubTOOLAREA:SAPLWB_CUSTOMIZING:0400/radRSEUMOD-TBALV_GRID")
          If ALVGridView.Selected = vbFalse Then
            ALVGridView.select()
          End If
          session.findById("wnd[1]/tbar[0]/btn[0]").press       
        Set BenutzerPar = Nothing
        Set Einstellungen = Nothing
        Set Menu = Nothing

      '-Get rows and columns--------------------------------------------
        Set table = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
        Rows = table.RowCount() - 1
        Cols = table.ColumnCount() - 1

      '-Write the table to a CSV file-----------------------------------
        Set oFile = CreateObject("Scripting.FileSystemObject")
        If IsObject(oFile) Then
          Set SFlightFile = oFile.CreateTextFile(FileName, True)
          If IsObject(SFlightFile) Then

            '-Get the title of all columns in the first line------------
              Set Columns = table.ColumnOrder()
              For j = 0 To Cols
                If j = Cols Then
                  SFlightFile.Write(CStr(Columns(j)))
                Else
                  SFlightFile.Write(CStr(Columns(j)) & ";")
                End If
              Next
              SFlightFile.WriteLine("")
 

            For i = 0 To Rows
              For j = 0 To Cols
                If j = Cols Then
                  SFlightFile.Write(table.GetCellValue(i, _
                    CStr(Columns(j))))
                Else
                  SFlightFile.Write(table.GetCellValue(i, _
                    CStr(Columns(j))) & ";")
                End If
              Next

              '-Each 32 lines actualize the grid------------------------
                If i Mod 32 = 0 Then
                  table.SetCurrentCell i, CStr(Columns(0))
                  table.firstVisibleRow = i
                End If

              '-Carriage and return after a line------------------------
                If i <> Rows Then
                  SFlightFile.WriteLine("")
                End If

            Next
            SFlightFile.Close

          End If
        End If

        Set ALVGridView = Nothing
        Set Columns = Nothing
        Set table = Nothing
    End Sub

  '-Main----------------------------------------------------------------
    If Not IsObject(application) Then
      Set SapGuiAuto = GetObject("SAPGUI")
      Set application = SapGuiAuto.GetScriptingEngine
    End If

    If Not IsObject(connection) Then
      Set connection = application.Children(0)
    End If


    If Not IsObject(session) Then
      Set session = connection.Children(0)
    End If

    '-Read the table SFLIGHT in a file----------------------------------
      ReadTableInFile "SFLIGHT", "C:\\Dummy\\SFlight.csv"

'-End-------------------------------------------------------------------

Viewing all articles
Browse latest Browse all 522

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>