-1

I have a script recorded from SAP ERP via SAP GUI, to run transaction code VA05. I need to run this script each day (I am fine achieving this) and I need the start date to always be the first date of the current month.

My script is below. What should I put in the VB script to replace "01.03.2021" so I always get the first date in the current month please?

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "va05"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").text = "01.03.2021"       '<=== HERE
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").setFocus
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
UKCLA
  • 1
  • 1
    There are in fact several questions here, which have already answers in Stack Overflow: how to [get the current date and how to format a date](https://stackoverflow.com/questions/9505408/formatting-the-current-date), how to extract text portions, how to concatenate date and texts... – Sandra Rossi Mar 18 '21 at 13:53

1 Answers1

0

Replace your string "01.03.2021" with this:

Format(DateSerial(Year(Date), Month(Date), 1), "dd.MM.yyyy")

Date function will give you date for actual day

Year function will extract year from date as integer

Month function will extract month from date as integer

DateSerial function will return Date to you based on arguments(year, month, day)

And finally Format function will format your date into desired format (dd.MM.yyyy) in your case.

So your code should look like this:

session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "va05"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").text = Format(DateSerial(Year(Date), Month(Date), 1), "dd.MM.yyyy")  
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").setFocus
session.findById("wnd[0]/usr/ctxtVBCOM-AUDAT").caretPosition = 10
session.findById("wnd[0]").sendVKey 0

Keep in mind that you can create a function for this (GetFirstDayOfCurrentMonth) if you want to use it in more than one place. The function might look something like this:

Function GetFirstDayOfCurrentMonth() As Date
    ' Lets say today is 18/03/2021 ( dd/MM/yyyy ) then
    ' variables y will be 2019 and variable m will be 3
    Dim y As Integer: y = year(Date)
    Dim m As Integer: m = month(Date)
    
    ' Variable firstDay is of type Date
    ' and its value is going to be 01/03/2021
    Dim firstDay As Date: firstDay = DateSerial(y, m, 1)
    
    GetFirstDayOfCurrentMonth = firstDay
End Function
eren
  • 708
  • 8
  • 20