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")
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Is this the line that specifies the date range?
Code:
Session.findById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20190215,20190215"
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,889
Messages
6,175,223
Members
452,620
Latest member
dsubash

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top