Skip to Content
avatar image
Former Member

Excel to SAP Using VBscript - Invalid Character error

Hi Experts,

My purpose is to utilize VBscript to automatically create multiple roles (via transaction PFCG) based on an Excel input.

I recently found out about VBscript for SAP so I'm not that good in VB coding yet.

I have followed this tutorial, credits to Mariano Cabalen for this:

Anyway, I've already recorded the process, and my excel file (which contains only two columns) is ready.

When I press the play button:

I encounter this error:

My question is, what could be the cause of this error? I'm sure this is not a syntax problem as I've religiously followed the guidelines of the tutorial.

Attaching the code here:

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


Set objExcel = GetObject(,”Excel.Application”) 
Set objWrkbk = objExcel.OpenWorkbook("C:\Users\sularte\Downloads\Test_Role_Creation.xlsx")
Set objSheet = Sheets("Sheet1") 


For i = 2 to objSheet.UsedRange.Rows.Count 
	session.findById("wnd[0]/tbar[0]/okcd").text = "/npfcg"
	session.findById("wnd[0]").sendVKey 0
	session.findById("wnd[0]/usr/ctxtAGR_NAME_NEU").text = COL1
	session.findById("wnd[0]/usr/ctxtAGR_NAME_NEU").caretPosition = 16
	session.findById("wnd[0]/usr/btn%#AUTOTEXT003").press
	session.findById("wnd[0]/usr/txtS_AGR_TEXTS-TEXT").text = COL2
	session.findById("wnd[0]/usr/txtS_AGR_TEXTS-TEXT").caretPosition = 9
	session.findById("wnd[0]").sendVKey 11
	session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB9").select
	session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB9/ssubSUB1:SAPLPRGN_TREE:0321/cntlTOOL_CONTROL/shellcont/shell").pressButton "TB03"
	session.findById("wnd[1]/usr/tblSAPLPRGN_WIZARDCTRL_TCODE/ctxtS_TCODES-TCODE[0,0]").text = "su01d"
	session.findById("wnd[1]/usr/tblSAPLPRGN_WIZARDCTRL_TCODE/ctxtS_TCODES-TCODE[0,0]").caretPosition = 5
	session.findById("wnd[1]").sendVKey 0
	session.findById("wnd[1]/tbar[0]/btn[19]").press
	session.findById("wnd[0]").sendVKey 11
	session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB5").select
	session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB5/ssubSUB1:SAPLPRGN_TREE:0350/btnEXPERTE").press
	session.findById("wnd[0]/usr/lbl[5,5]").setFocus
	session.findById("wnd[0]/usr/lbl[5,5]").caretPosition = 1
	session.findById("wnd[0]").sendVKey 2
	session.findById("wnd[0]/usr/lbl[32,2]").setFocus
	session.findById("wnd[0]/usr/lbl[32,2]").caretPosition = 0
	session.findById("wnd[0]").sendVKey 2
	session.findById("wnd[1]/tbar[0]/btn[0]").press
	session.findById("wnd[0]").sendVKey 11
	session.findById("wnd[1]/tbar[0]/btn[0]").press
	session.findById("wnd[0]/tbar[1]/btn[17]").press
	session.findById("wnd[0]/tbar[0]/btn[3]").press
	session.findById("wnd[0]/tbar[0]/btn[3]").press
next
msgbox “Process Completed”


Set objSheet = Nothing
objWrkbk.close
Set objWrkbk = Nothing
objExcel.close
Set objExcel = Nothing

Thanks in advance!

-Bagani

2.png (37.5 kB)
error-sap-1.png (6.1 kB)
3.png (7.9 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • avatar image
    Former Member
    Mar 29, 2017 at 12:43 PM

    Hello,

    in the tutorial you mention there are two code blocks which must be put in your script.

    I don't see this lines in you code. Check the tutorial and look for e.g. "Block of code: Block A"

    kind regards,

    Bernhard

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    May 05, 2017 at 02:19 PM

    Hi!

    I´m not a SAP expert, but as you, I´ve tried to work with the code posted by Mariano but I could´t, the same error pops up...

    After a few hours searching on google...I found this code on SlideShare, I combined and adapted both

    Now it´s working for me (on different TCODE)

    Here I´ll copy-paste your code with the modifications, try with this and I hope it be helpful

    Greetings

    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

    Dim objExcel,objWorkbook
    Dim objSheet, intRow, i
    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.open ("C:\Users\sularte\Downloads\Test_Role_Creation.xlsx")
    Set objSheet = objExcel.Worksheets(1)
    Row = 2

    session.findById("wnd[0]").maximize
    session.findById("wnd[0]/tbar[0]/okcd").text = "/npfcg"
    session.findById("wnd[0]").sendVKey 0

    Do Until objSheet.cells(Row,1).value=""

    session.findById("wnd[0]/usr/ctxtAGR_NAME_NEU").text = objSheet.Cells(Row,1).Value
    session.findById("wnd[0]/usr/ctxtAGR_NAME_NEU").caretPosition = 16
    session.findById("wnd[0]/usr/btn%#AUTOTEXT003").press
    session.findById("wnd[0]/usr/txtS_AGR_TEXTS-TEXT").text = objSheet.Cells(Row,2).Value
    session.findById("wnd[0]/usr/txtS_AGR_TEXTS-TEXT").caretPosition = 9
    session.findById("wnd[0]").sendVKey 11
    session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB9").select
    session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB9/ssubSUB1:SAPLPRGN_TREE:0321/cntlTOOL_CONTROL/shellcont/shell").pressButton "TB03"
    session.findById("wnd[1]/usr/tblSAPLPRGN_WIZARDCTRL_TCODE/ctxtS_TCODES-TCODE[0,0]").text = "su01d"
    session.findById("wnd[1]/usr/tblSAPLPRGN_WIZARDCTRL_TCODE/ctxtS_TCODES-TCODE[0,0]").caretPosition = 5
    session.findById("wnd[1]").sendVKey 0
    session.findById("wnd[1]/tbar[0]/btn[19]").press
    session.findById("wnd[0]").sendVKey 11
    session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB5").select
    session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpTAB5/ssubSUB1:SAPLPRGN_TREE:0350/btnEXPERTE").press
    session.findById("wnd[0]/usr/lbl[5,5]").setFocus
    session.findById("wnd[0]/usr/lbl[5,5]").caretPosition = 1
    session.findById("wnd[0]").sendVKey 2
    session.findById("wnd[0]/usr/lbl[32,2]").setFocus
    session.findById("wnd[0]/usr/lbl[32,2]").caretPosition = 0
    session.findById("wnd[0]").sendVKey 2
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[0]").sendVKey 11
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[0]/tbar[1]/btn[17]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press
    session.findById("wnd[0]/tbar[0]/btn[3]").press

    Row = Row + 1
    Loop
    objExcel.Quit
    Set objExcel = Nothing
    Set objWorkbook = Nothing
    Set objSheet = Nothing
    msgbox (“Process Completed”)

    Add comment
    10|10000 characters needed characters exceeded