Tuesday 23 April 2019

Automating Period Closing in Test Environment using Excel and VBScript

Q:  Is there a way to automate the closing in MMPV? Say the current posting period in the test environment for the company code you need to test is in 2011 and it's now 2019.


A: Check out the article by Mariano Cabalen. After I did the recording and made a few adjustments to his code, it ran perfectly.

Warning: If you want to use this code ensure have only the SAP Test Environment open, and only the Excel sheet you need to use open. The program looks at the active Worksheet.
  1. Login to SAP Test Environment
  2. Format your Excel similar to mine, screenshot below.
  3. Copy and paste my code to notepad and change the extension to VBS. 
  4. Double click the file
  5. A dialog box will appear with the message "A script is trying to attach to the GUI". 
  6. Click Ok. The script will then run. Sit back and watch a ghost use your computer and do you work☺



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
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize

Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.ActiveWorkbook.ActiveSheet
For i = 2 to objSheet.UsedRange.Rows.Count
columnA = Trim(CStr(objSheet.Cells(i, 1).Value))
columnB = Trim(CStr(objSheet.Cells(i, 2).Value))
columnC = Trim(CStr(objSheet.Cells(i, 3).Value))
columnD = Trim(CStr(objSheet.Cells(i, 4).Value))

session.findById("wnd[0]/tbar[0]/okcd").text = "/n mmpv"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtI_VBUKR").text = columnA
session.findById("wnd[0]/usr/ctxtI_BBUKR").text = columnB
session.findById("wnd[0]/usr/txtI_LFMON").text = columnC
session.findById("wnd[0]/usr/txtI_LFGJA").text = columnD
session.findById("wnd[0]/usr/txtI_LFGJA").setFocus
session.findById("wnd[0]/usr/txtI_LFGJA").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]").sendVKey 0
Next