Skip to main content
Community site session details

Community site session details

Session Id : u/5L30aMaKpIp/pVhvLc+n
Power Automate - Power Automate Desktop
Unanswered

SAP VBS Code with Variables

Like (0) ShareShare
ReportReport
Posted on 23 May 2022 10:03:44 by 4

Hi all,

 

I'm running a VBS script in SAP using variables (these last are dates, coming from a JS script, as an output), here below the code:

 

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
session.findById("wnd[0]/tbar[0]/okcd").text = ZRSVC001_D4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtV-LOW").text = "/LTP_H"
session.findById("wnd[1]/usr/txtENAME-LOW").text = ""
session.findById("wnd[1]/usr/txtENAME-LOW").setFocus
session.findById("wnd[1]/usr/txtENAME-LOW").caretPosition = 0
session.findById("wnd[1]").sendVKey 8
session.findById("wnd[0]/usr/ctxtJOBDATE").text = %CurrentDate%
session.findById("wnd[0]/usr/ctxtJOBDATE").setFocus
session.findById("wnd[0]/usr/ctxtJOBDATE").caretPosition = 10
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = "TOT"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "0"
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").select
session.findById("wnd[1]/usr/ctxtDY_PATH").text = "C:\Users\user\Documents\Downloads"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "Test.xlsx"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 9
session.findById("wnd[1]").sendVKey 11

 

When I'm running this script without using variables, but simply using string or integer, it works fine, otherwise I'm getting this error as soon as the script starts:


Microsoft VBScript compilation error: Expected Statement

 

If anyone could help

 

Appreciate

 

Thanks

  • SjaakS Profile Picture
    22 on 30 May 2022 at 12:01:13
    Re: SAP VBS Code with Variables

    I think I know what the issue is.

    If I look at the first screenshot, in your last message, I see a second line that might indicate some whitespace.

    Try using the following action on your variable, this will delete the whitespace that might be responsible for your issue.

    2022-05-30 13_58_36-TrimText.png

  • AndreVe Profile Picture
    4 on 25 May 2022 at 14:03:46
    Re: SAP VBS Code with Variables

    %CurrentDateDot% (or %CurrentDate% it's the same) is coming from another Desktop Flow (let's call it A) where I run all JS for dates, timestap, etc and these are being generated and stored as Output Variables; these latter are being recalled when I run the Desk Flow "A" in the Desk Flow "B", here the screenshot after the run:

     

    AndreVe_0-1653484376252.png

     

     

    this is the Java creating the date:

     

    var today = new Date();
    var dd = today.getDate();

    var mm = today.getMonth()+1;
    var yyyy = today.getFullYear();
    if(dd<10)
    {
    dd='0'+dd;
    }

    if(mm<10)
    {
    mm='0'+mm;
    }
    Result = yyyy+'.'+mm+'.'+dd;
    WScript.Echo (Result);

     

     

    I tried another way: I set a variable and put in in the VBS script -> worked fine

     

    AndreVe_1-1653486867055.png

     

    Moreover when I use those variables, coming from the JS, in any other context (writing in Notepad, or any platform) they are working just fine

     

    Thanks

  • SjaakS Profile Picture
    22 on 25 May 2022 at 10:31:37
    Re: SAP VBS Code with Variables

    When using the CurrentDate variable in Datetime format, it might be possible that you receive following script error: SAP Frontend Server: The method got an invalid argument.

    After using the 'Convert datetime to text' action (Standard format, Short date), the formatted variable worked like a charm.

    I added screenshots from the FBL5N-test and the script that I used.

     

    2022-05-25 12_25_42-Macro Test _ Power Automate.png

    2022-05-25 12_25_12-Run VBScript.png

  • VJR Profile Picture
    7,635 on 25 May 2022 at 03:20:40
    Re: SAP VBS Code with Variables

    What are the valid values you were able to pass successfully as hard coded? The one that you mentioned string or integer.

     

    What is the datatype of %CurrentDate%?

    Does it expect a Text and you are passing a DateTime or viceversa?

    See if you need to do a Convert Text to DateTime or DateTime to Text before passing it to the vbscript 

     

    Also does it need only a specific date format eg; 25.05.2022 ?

     

    Check the video in this link. Something might ring a bell as to what is going wrong.

    https://docs.microsoft.com/en-us/power-automate/guidance/rpa-sap-playbook/vbscript-based-sap-gui-automation-overview

     

  • AndreVe Profile Picture
    4 on 24 May 2022 at 12:54:57
    Re: SAP VBS Code with Variables

    I have already tried to use double quotes, double %, but getting same error 

  • VJR Profile Picture
    7,635 on 24 May 2022 at 03:40:02
    Re: SAP VBS Code with Variables

    Within a vbscript PAD variables are referred using double quotes around it

     

    "%CurrentDate%"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Announcing our 2025 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for…

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Loading complete