cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AndreVe
Regular Visitor

SAP VBS Code with Variables

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

6 REPLIES 6
VJR
Super User
Super User

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

 

"%CurrentDate%"

AndreVe
Regular Visitor

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

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-aut...

 

SjaakS
Frequent Visitor

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

AndreVe
Regular Visitor

%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
Frequent Visitor

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

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

MPA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

Learn to digitize and optimize business processes and connect all your applications to share data in real time.

Power Automate Designer Feedback_carousel.jpg

Help make Flow Design easier

Are you new to designing flows? What is your biggest struggle with Power Automate Designer? Help us make it more user friendly!

Top Solution Authors
Top Kudoed Authors
Users online (2,912)