Home > Net >  Initialize a variable with the value of a screen field
Initialize a variable with the value of a screen field

Time:07-21

I've got a script going through MM03 transaction. There are 2 screen fields Period (Month) and Year that I want to copy into variables.

There is no chance to do this with export or print, because it is a not changeable box.

I tried to record using SAP GUI Recorder, I click in a field box and press Ctrl A then Ctrl C, it isn't recorded, so I have to do this part with VBA. I tried it like this, but it doesn't fit.

Option Explicit
Public SapGuiAuto
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
    
Sub SAPCustomerReport()
    
    Set SapGuiAuto = GetObject("SAPGUI")
    Set objGui = SapGuiAuto.GetScriptingEngine
    Set objConn = objGui.Children(0)
    Set session = objConn.Children(0)
    
    Dim DataObj As New DataObject
    Dim month As String
    Dim year As String
    
    Application.OnKey "^A", ""
    Application.OnKey "^C", ""
    
    session.FindById("wnd[0]").Maximize
    session.FindById("wnd[0]/tbar[0]/okcd").Text = "TransactionExample"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = "MaterialExample"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27").Select
    session.FindById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "PlaceExample"
    session.FindById("wnd[1]/tbar[0]/btn[0]").Press
    session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2953/txtMBEW-PPRDL").SetFocus
    
    
      SendKeys "^A"
      SendKeys "^C"
      DataObj.GetFromClipboard
      month = DataObj.GetText
    
    Set SapGuiAuto = Nothing
    Set objGui = Nothing
    Set objConn = Nothing
    Set session = Nothing
    
End Sub

CodePudding user response:

Based on your code the following code will retrieve the month and year of the current period in the tab Costing2

Option Explicit

Public SapGuiAuto
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession

Sub SAPCustomerReport()

    Set SapGuiAuto = GetObject("SAPGUI")
    Set objGui = SapGuiAuto.GetScriptingEngine
    Set objConn = objGui.Children(0)
    Set session = objConn.Children(0)

    Dim month As String
    Dim year As String

    session.FindById("wnd[0]").Maximize
    session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nMM03"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = "xxxxx"
    session.FindById("wnd[0]").SendVKey 0
    session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27").Select
    session.FindById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "xxxx"
    session.FindById("wnd[1]/tbar[0]/btn[0]").Press
    
    month = session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2951/txtMBEW-PPRDL").Text
    year = session.FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2951/txtMBEW-PDATL").Text

End Sub

The question is how to get at the IDs of the screen elements in question in order to use findByID for retrieving the field content. I usually take the Tracker

Tracker: Homepage Blog entry

SAPGUI Scripting Documentation

A nicer resp. more readable version might look like that

Option Explicit

Public SapGuiAuto
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession

Sub SAPCustomerReport()

    Set SapGuiAuto = GetObject("SAPGUI")
    Set objGui = SapGuiAuto.GetScriptingEngine
    Set objConn = objGui.Children(0)
    Set session = objConn.Children(0)

    Dim month As String
    Dim year As String

    With session
    
        .FindById("wnd[0]").Maximize
        .FindById("wnd[0]/tbar[0]/okcd").Text = "/nMM03"
        .FindById("wnd[0]").SendVKey 0
        .FindById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = "xxxxxx"
        .FindById("wnd[0]").SendVKey 0
        .FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27").Select
        .FindById("wnd[1]/usr/ctxtRMMG1-WERKS").Text = "xxxx"
        .FindById("wnd[1]/tbar[0]/btn[0]").Press
        
        Dim guiArea As GuiVComponent
        Set guiArea = .FindById("wnd[0]/usr/tabsTABSPR1/tabpSP27/ssubTABFRA1:SAPLMGMM:2000/subSUB2:SAPLMGD1:2951")
        
    End With
    
    With guiArea
        month = .FindById("txtMBEW-PPRDL").Text
        year = .FindById("txtMBEW-PDATL").Text
    End With

End Sub
  • Related