cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble with VBS to start new session.

Former Member
0 Kudos

Hello,

I am trying to run VBS to reset the password through VBS with a input file.

However, script start a session and run fine on first row of data, but it is not starting a session on second run.

Your help is greatly appreciated.

Klee

==================================== Input file ====================================

[c:\testinput.txt as my input file : UserID | Current Password | New Password | Client | SystemName]

TESTA|Aloha123|Aloha777|100|D01_DEV

TESTB|Aloha123|Aloha777|100|D01_DEV

.......

====================================Code=======================================

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("c:\testinput.txt", ForReading)

Const ForReading = 1

Dim arrFileLines()

i = 0

Do Until objFile.AtEndOfStream

Redim Preserve arrFileLines(i)

arrFileLines(i) = objFile.ReadLine

i = i + 1

Loop

objFile.Close

For Each strLine in arrFileLines

MyString = strLine

MyArray = Split(MyString, "|", -1, 1)

'Msgbox MyArray(0) 'UserID

'Msgbox MyArray(1) 'CurrentPassword

'Msgbox MyArray(2) 'NewPassword

'MSgbox MyArray(3) 'Client

'MSgbox MyArray(4) 'System

If Not IsObject(SAPguiApp) Then

Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")

End If

If Not IsObject(Connection) Then

Set Connection = SAPguiApp.OpenConnection(MyArray(4), True) 'SystemName

End If

If Not IsObject(session) Then

Set session = Connection.Children(0)

End If

session.findById("wnd[0]/usr/txtRSYST-MANDT").text = MyArray(3) 'Client

session.findById("wnd[0]/usr/txtRSYST-MANDT").setFocus

session.findById("wnd[0]/usr/txtRSYST-MANDT").caretPosition = 2

session.findById("wnd[0]/usr/txtRSYST-BNAME").text = MyArray(0) 'Input_UserID

session.findById("wnd[0]/usr/txtRSYST-BNAME").setFocus

session.findById("wnd[0]/usr/txtRSYST-BNAME").caretPosition = 4

session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = MyArray(1) 'Input_CurrentPassword

session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus

session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 6

session.findById("wnd[0]/tbar[1]/btn[5]").press

session.findById("wnd[1]/usr/pwdRSYST-NCODE").text = MyArray(2) 'Input_NewPassword

session.findById("wnd[1]/usr/pwdRSYST-NCOD2").text = MyArray(2) 'Input_NewPassword

session.findById("wnd[1]/usr/pwdRSYST-NCOD2").setFocus

session.findById("wnd[1]/usr/pwdRSYST-NCOD2").caretPosition = 8

session.findById("wnd[1]").sendVKey 0

session.findById("wnd[0]/tbar[0]/okcd").text = "/nex"

session.findById("wnd[0]").sendVKey 0

Next

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

ScriptMan,

Your script works like a charm!.

And I find lots of useful tips from your messages.

Thank you.

btw, is there any way I can have the input file with EXEL format?

Thanks again.

Klee

script_man
Active Contributor
0 Kudos

This message was moderated.

Former Member
0 Kudos

Thanks again for your reply with a magic answer.

I click on one of radio button below your name. hope that is a way to add point to you. (sorry I am not familiar with it

script_man
Active Contributor
0 Kudos

Hi Klee,

I think there are several solutions to the issue. One of them may look like this:


set wshell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile = objFSO.OpenTextFile("c:\testinput.txt", ForReading) 
Const ForReading = 1
Dim arrFileLines() 
i = 0 
Do Until objFile.AtEndOfStream 
Redim Preserve arrFileLines(i) 
arrFileLines(i) = objFile.ReadLine 
i = i + 1 
Loop 
objFile.Close 
For Each strLine in arrFileLines 
MyString = strLine 
MyArray = Split(MyString, "|", -1, 1) 
'This is a complicated version if spaces are considered.
'wshell.run chr(34) & "c:\script.vbs" & chr(34) & " " & chr(34) & MyArray(0) & chr(34) & " " & chr(34) & MyArray(1) & chr(34) & " " & chr(34) & MyArray(2) & chr(34) & " " & chr(34) & MyArray(3) & chr(34) & " " & chr(34) & MyArray(4) & chr(34),1,true
'This is a simple version with no spaces in the string.
wshell.run "c:\script.vbs" & " " & MyArray(0) & " " & MyArray(1) & " " & MyArray(2) & " " & MyArray(3) & " " & MyArray(4),1,true
Next

script.vbs:


'msgbox wscript.arguments(0) & " " & wscript.arguments(1) & " " & wscript.arguments(2) & " " & wscript.arguments(3) & " " & wscript.arguments(4)
if wscript.arguments.count = 5 then
 If Not IsObject(SAPguiApp) Then
    Set SAPguiApp = CreateObject("Sapgui.ScriptingCtrl.1")
 End If
 If Not IsObject(Connection) Then
    Set Connection = SAPguiApp.OpenConnection(wscript.arguments(4), True) 'SystemName
 End If
 If Not IsObject(session) Then
    Set session = Connection.Children(0)
 End If
 session.findById("wnd[0]/usr/txtRSYST-MANDT").text = wscript.arguments(3) 'Client
 session.findById("wnd[0]/usr/txtRSYST-BNAME").text = wscript.arguments(0) 'Input_UserID
 session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = wscript.arguments(1) 'Input_CurrentPassword
 session.findById("wnd[0]/tbar[1]/btn[5]").press
 session.findById("wnd[1]/usr/pwdRSYST-NCODE").text = wscript.arguments(2) 'Input_NewPassword
 session.findById("wnd[1]/usr/pwdRSYST-NCOD2").text = wscript.arguments(2) 'Input_NewPassword
 session.findById("wnd[1]").sendVKey 0
 session.findById("wnd[0]/tbar[0]/okcd").text = "/nex"
 session.findById("wnd[0]").sendVKey 0
end if

Regards,

ScriptMan

Edited by: ScriptMan on Nov 23, 2010 11:42 AM