Date Entry of SAP through VBA

zxcvbc

New Member
Joined
Feb 20, 2019
Messages
6
Hi All,

I have a macro which run automatically at a specific time which connect SAP and run the SAP script. The input of the starting date is expected to enter the current date deduct by 7 days.
Below is the code which the starting date need to be specified a date. I am not able to use FORMAT(DATE) at starting date. Thank you.

Code:
Sub aa()
    Application.DisplayAlerts = False


Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus

Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon")
    Application.Wait Now + TimeValue("0:00:01")
Loop


If Not IsObject(SAPguiApp) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set SAPguiApp = SapGuiAuto.GetScriptingEngine
End If

If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.Openconnection("CP0 [Back Office]", True)
End If

If Not IsObject(Session) Then
    Set Session = Connection.Children(0)
End If

If IsObject(WScript) Then
    WScript.ConnectObject Session, "on"
    WScript.ConnectObject SAPguiApp, "on"
End If

On Error Resume Next
    If Session.findById("wnd[1]").Text = "License Information for Multiple Logon" Then
        Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
        Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").SetFocus
        Session.findById("wnd[1]").sendVKey 0
    End If

Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "T-CODE"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]").sendVKey 4
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20190215"

Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20190215,20190215"

Session.findById("wnd[0]/usr/tabsTABSTRIP_TABSTRIP/tabpMQML/ssub%_SUBSCREEN_TABSTRIP:SAPLZQN_MON_BROWSER:0510/ctxtS_QMDAT-HIGH").Text = Format(Date - 1, "dd.mm.yyyy")
 

Some videos you may like

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
1,768
Is this the line that specifies the date range?
Code:
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20190215,20190215"
 

Jeffrey Mahoney

Well-known Member
Joined
May 31, 2015
Messages
1,768
Seems to me by the other script lines and based on my experience with SAP, you need to provide a date in the format yyyymmdd. Date is a reserved word in VBA for a type of variable. NOW() is used to get the current date and time. You did ask for the date to be 7 days prior, right?

Code:
Sub aa()
    
  Dim A As String
  Dim D As Date
  
    Application.DisplayAlerts = False




Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus


Set WSHShell = CreateObject("WScript.Shell")
Do Until WSHShell.AppActivate("SAP Logon")
    Application.Wait Now + TimeValue("0:00:01")
Loop




If Not IsObject(SAPguiApp) Then
    Set SapGuiAuto = GetObject("SAPGUI")
    Set SAPguiApp = SapGuiAuto.GetScriptingEngine
End If


If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.Openconnection("CP0 [Back Office]", True)
End If


If Not IsObject(Session) Then
    Set Session = Connection.Children(0)
End If


If IsObject(WScript) Then
    WScript.ConnectObject Session, "on"
    WScript.ConnectObject SAPguiApp, "on"
End If


On Error Resume Next
    If Session.findById("wnd[1]").Text = "License Information for Multiple Logon" Then
        Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
        Session.findById("wnd[1]/usr/radMULTI_LOGON_OPT2").SetFocus
        Session.findById("wnd[1]").sendVKey 0
    End If


Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "T-CODE"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]").sendVKey 4
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = "20190215"


Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20190215,20190215"


D = Now() - 7
A = Year(D) & Format(Month(D), "00") & Format(Day(D), "00")
Session.findById("wnd[0]/usr/tabsTABSTRIP_TABSTRIP/tabpMQML/ssub%_SUBSCREEN_TABSTRIP:SAPLZ N_MON_BROWSER:0510/ctxtS_QMDAT-HIGH").Text = A
 

zxcvbc

New Member
Joined
Feb 20, 2019
Messages
6
I tried the solution you given, the date field in SAP still return the current date for me.
 

ps01

New Member
Joined
Aug 13, 2020
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi, how can I manage interval.. if I want every time i run macro there will be interval ...from today to today.

Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20190215,20190215"

Thans Peter
 

Watch MrExcel Video

Forum statistics

Threads
1,109,043
Messages
5,526,423
Members
409,701
Latest member
nitmani

This Week's Hot Topics

Top