on 10-04-2010 6:10 PM
Hi Experts - I ran into a problem when trying to feed a filename into an open file dialog. I used this approach in the past with an "Open" dialog and thought same would work for "Import file" dialog box.
My main script is below:
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 = "/niw32"
session.findById("wnd[0]").sendVKey 0
OrdNum = "7237602"
set Wshell = CreateObject("WScript.Shell")
session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
'>>> these 2 lines above bring up the dialog box <<<<<
Wshell.run "c:\tmp\Scandf.vbs",1,False
'>> after the filename is dropped in the dialog box by the Scandf.vbs script it should continue with the process <<
session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/subSUB_KOPF:SAPLCOIH:1102/btn%#AUTOTEXT001").press
session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").selected = true
session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").setFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[0]/btn[11]").press
I am calling a script called Scandf.vbs where I have the filename I want to drop into the dialog box. This is the Scandf.vbs:
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
set Wshell = CreateObject("WScript.Shell")
MsgBox "in sub"
Do
bWindowFound = Wshell.AppActivate("Import file")
WScript.Sleep 1000
Loop Until bWindowFound
bWindowFound = Wshell.AppActivate("Import file")
if (bWindowFound) Then
Wshell.appActivate "Import file"
WScript.Sleep 100
Wshell.sendkeys "c:\EQ2814.txt"
WScript.Sleep 100
Wshell.sendkeys ""
end if
EQ2814.txt is the filename I want to drop in.
THe cursor is in the filename field and waiting for input. It just sits there after the dialog box is called and does not drop in the filename.
Can anybody see what I am doing wrong?
Thanks
Umur
Hi Umur,
Unfortunately I do not have access to the transaction iw32. But you could try the following.
In the main script should be called Scandf.vbs previously:
. . .
set Wshell = CreateObject("WScript.Shell")
Wshell.run "c:\tmp\Scandf.vbs",1,False
session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
. . .
Scandf.vbs does not need the first 14 rows:
set Wshell = CreateObject("WScript.Shell")
MsgBox "in sub"
Do
bWindowFound = Wshell.AppActivate("Import file")
WScript.Sleep 1000
Loop Until bWindowFound
'-> new
Msgbox "Window = 'Import file' found"
bWindowFound = Wshell.AppActivate("Import file")
if (bWindowFound) Then
Wshell.appActivate "Import file"
WScript.Sleep 100
Wshell.sendkeys "c:\EQ2814.txt"
WScript.Sleep 100
Wshell.sendkeys "{ENTER}"
'-> new
WScript.Sleep 100
end if
Otherwise one would have to examine the same level as the windows. For example:
"Import file" # "Import file"
Between the words Import and file are 2 spaces right.
Regards,
ScriptMan
Edited by: ScriptMan on Oct 5, 2010 9:57 AM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi ScriptMan - Thanks for the reply. I tried your suggestion but did not work. I think the problem is with this statement:
Wshell.run "c:\tmp\Scandf.vbs",1,False
When the script hits this line I get an "Unknown error". I move the line around and at that exact spot I get the same error. It is without a doubt this statement that causes the error. I check my c:\tmp folder and the file is there. I took out the first 14 lines like you suggested and the rest of the code is as you had on your message. It does not even display the message "in sub" which is the 2nd statment right after entering the scandf.vbs. This tells me it is not even getting into this script. The 'run' line looks good. SO I am stumped as to why it fails to call the scandf.vbs. As for the "Import file" it is ok with one space - I checked that one but it doesn't even go that far into the script to fail on that.
Do you see an error with this call?:
Wshell.run "c:\tmp\Scandf.vbs",1,False
Best regards
Umur
Hi Umur,
I also see no error in the 'run' line. You can try the following:
1. Please disable in the main script the line with Wshell.run "c:\tmp\scandf.vbs",1, False
2. Then start the main script.
3. Then you'll see the dialog with Open File, please run the script scandf.vbs manually.
4. All other steps are then dependent on the published errors at this point.
Regards,
ScriptMan
Hi ScriptMan - getting closer I think. When I removed that line and ran it to "Import file" step and then manually started Scandf.vbs, it worked perfectly with the code and displayed the 2 MSgBox'es, then compelted rest of the script from the main routine:
Set Wshell = CreateObject("WScript.Shell")
MsgBox "in sub"
Do
bWindowFound = Wshell.AppActivate("Import file")
WScript.Sleep 1000
Loop Until bWindowFound
Msgbox "Window = 'Import file' found"
bWindowFound = Wshell.AppActivate("Import file")
if (bWindowFound) Then
Wshell.appActivate "Import file"
WScript.Sleep 100
Wshell.sendkeys "" Wshell.sendkeys ""
Wshell.sendkeys "" Wshell.sendkeys ""
Wshell.sendkeys "c:\EQ2814.txt"
WScript.Sleep 100
Wshell.sendkeys "" WScript.Sleep 100 end if Then I tried making Scandf.vbs a subroutine within the main, placed the code as follows but that still stopped at the "Import file" panel. I don't think it is a matter of recognizing the panel since when I do it manually it does see it and drop in the file name: 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 = "/niw32" session.findById("wnd[0]").sendVKey 0 set Wshell = CreateObject("WScript.Shell") OrdNum = "7237602" session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").text = OrdNum session.findById("wnd[0]").sendVKey 0 'Wshell.run "c:\tmp\Scandf.vbs",1,False session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX" session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA" session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA" SecondFile session.findById("wnd[0]/usr/subSUB_ALL:SAPLCOIH:3001/ssubSUB_LEVEL:SAPLCOIH:1100/subSUB_KOPF:SAPLCOIH:1102/btn%#AUTOTEXT001").press session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").selected = true session.findById("wnd[1]/usr/sub:SAPLBSVA:0201[1]/chkJ_STMAINT-ANWSO[0,0]").setFocus session.findById("wnd[1]/tbar[0]/btn[0]").press session.findById("wnd[0]/tbar[0]/btn[11]").press Sub SecondFile Set Wshell = CreateObject("WScript.Shell") MsgBox "in sub" Do bWindowFound = Wshell.AppActivate("Import file") WScript.Sleep 1000 Loop Until bWindowFound Msgbox "Window = 'Import file' found" bWindowFound = Wshell.AppActivate("Import file") if (bWindowFound) Then Wshell.appActivate "Import file" WScript.Sleep 100 Wshell.sendkeys "c:\EQ2814.txt" WScript.Sleep 100 Wshell.sendkeys ""
WScript.Sleep 100
end If
End sub
Why do you think we can't get the scandf.vbs to trigger automatically?
Best regards
Umur
Hi Umur,
I am well satisfied that your program can run automatically. Place your following the subroutine call like:
. . .
SecondFile
session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX"
session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA"
session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"
. . .
Regards,
ScriptMan
Hi ScriptMan - I tried your suggestion and it complained about 'object required-Wscript'. Then I reverted back to the original concept of calling the file scandf.vbs through the run command, and this gives me 'unknown error' message. The code is now like this once again in the main program:
Wshell.run "c:\tmp\Scandf.vbs",1,False
session.findById("wnd[0]/titl/shellcont/shell").pressButton "%GOS_TOOLBOX"
session.findById("wnd[0]/shellcont/shell").pressContextButton "CREATE_ATTA"
session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"
I know scandf.vbs works because I can run it when the 'import file' panel comes up and it goes through.
But palcing the lime:
Wshell.run "c:\tmp\Scandf.vbs",1,False
before the toolbox block gives that unkinown erorr, and placing it after the toolbox block just hangs up the script - it waits for the filename to be entered on the dialog box.
It is pretty stange and I am not sure what the problem is.
Best Regards
Umur
Hi Umur,
How to start the main script? With Windows-Explorer, SAP or . . .
The variant with SecondFile, you could still develop.
Sub SecondFile
Set Wshell = CreateObject("WScript.Shell")
MsgBox "in sub"
Do
bWindowFound = Wshell.AppActivate("Import file")
' WScript.Sleep 1000
wshell.run "c:\tmp\sleep_1000.vbs",1,true
Loop Until bWindowFound
Msgbox "Window = 'Import file' found"
bWindowFound = Wshell.AppActivate("Import file")
if (bWindowFound) Then
Wshell.appActivate "Import file"
' WScript.Sleep 100
wshell.run "c:\tmp\sleep_100.vbs",1,true
Wshell.sendkeys "c:\EQ2814.txt"
' WScript.Sleep 100
wshell.run "c:\tmp\sleep_100.vbs",1,true
Wshell.sendkeys "{ENTER}"
' WScript.Sleep 100
wshell.run "c:\tmp\sleep_100.vbs",1,true
end If
End sub
sleep_1000.vbs:
wscript.sleep 1000
sleep_100.vbs:
wscript.sleep 100
Regards,
ScriptMan
Edited by: ScriptMan on Oct 6, 2010 4:02 PM
ScriptMan - one quick question as an extension of this script... can I send the filename (in this example "c:\EQ2814.txt") from the main program to the Scandf.vbs script so I can tell the pasting script which filename to use when main program is reading from a file in a loop? Is there a way to send a paramater from calling program to the called program?
Thanks
Umur
ScriptMan - I got this one figured out - in case some one else needs to use something like this:
(Sorry for many messages folks)
in the calling program:
FileName = "C:\EQ2814.txt"
Set Wshell = CreateObject("WScript.Shell")
Wshell.run "c:\tmp\Scandf.vbs " & FileName,1,False
In the called program:
Dim FileNam2
FileNam2 = WScript.Arguments.Item(0)
Wshell.sendkeys FileNam2
Best regards
Umur
Hi Umur,
I have the path of the upload file saved in the excel sheet for every document that has to be uploaded.
In the Scandf.vbs file, you have mentioned the path but what if I want scandf.vbs to automatically copy the path from the excel sheet and upload the file automatically.
Example (in excel sheet)
A B C D
1 Document No. Company Code Year File Path
2 1000000000 1111 2015 C:\test1.xls
3 1000000001 1111 2015 C:\test2.xls
I want Scandf.vbs to automatically take the path form the excel sheet - C:\test1.xls and C:\test2.xls
Could you please help me with this?
Thanks in advance!
Amit
Having the same problem too. I have the filepath inside my excel sheet. I got everything up and running (validations, loop, etc..) I just can't figure out how to do a code that will copy the path from my excel sheet, then paste it in the dialogbox in SAP
Public Sub AttachNow()
doc = ActiveCell.Value
attachment = ActiveCell.Offset(0, 1).Value 'this is the cell value where the filepath is copied
salesorg = Range("F3").Value
Dim current As Workbook
Set current = ActiveWorkbook
Dim ARlayout As String
Dim objExcel
Dim objSheet, intRow, i
Set objExcel = GetObject(, "Excel.Application")
Set objBook = objExcel.ActiveWorkbook
Set SapGuiAuto = GetObject("SAPGUI") 'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection
'Start the transaction to access FB03
session.findById("wnd[0]").resizeWorkingPane 235, 41, False
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nFB03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/txtRF05L-BELNR").Text = doc
session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").Text = salesorg
session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").SetFocus
session.findById("wnd[0]/usr/ctxtRF05L-BUKRS").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
'put code to paste path in dialog box here
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
End Sub
Hi,
can we run Scandf.vbs, inside of the main script like function or Sub file?
thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Umur,
Sendkeys is a Method to Send KeyCombinations like Enter, or The F-Keys to the Active Application
like in SAP the session.findById("wnd[0]").sendvKey 0
You can't "Cpoy - Paste" Complete Text by sendkeys
Perhaps you should try to split up your Text
dummy = "EQ1234.txt"
for i = 1 to len (dummy)
wShell.Sendkeys{mid(dummy(1,i,1))
next
Greetings
Thomas
Edited by: Thomas Br on Oct 4, 2010 7:40 PM
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks for the reply Thomas, but I think my trouble starts even before dropping the filename.. I tried with your code and it did not do anything different.. I think the issue is with getting the script to figure out that the "Inport file" panel is up and running so it can take action.. in the section of the called routine below.. you see the messagebox I inserted just to make sure it comes to this step:
.
.
set Wshell = CreateObject("WScript.Shell")
MsgBox "in sub"
Do
bWindowFound = Wshell.AppActivate("Import file")
WScript.Sleep 1000
Loop Until bWindowFound
.
.
but I don't see that message box pop-up - so I think the issue is with the main script that calls this sub-script after getting the dialog box up first with the 2 top lines immediately below:
.
.
session.findById("wnd[0]/titl/shellcont/shell").pressContextButton "%GOS_TOOLBOX"
session.findById("wnd[0]/titl/shellcont/shell").selectContextMenuItem "%GOS_PCATTA_CREA"
Wshell.run "c:\tmp\Scandf.vbs",1,False
I also made sure that the app name is correct as "Import file" by confirming from Task Manager.
Not sure why it would hang up after the Import file dialog is brought up.
Any thoughts?
Best regards
Umur
For those inquisitive minds out there, I confirmed that when it processes this step:
session.findById("wnd[0]/shellcont/shell").selectContextMenuItem "PCATTA_CREA"
and brings up the file import dialog box, that is where it hangs. It is almost like all processing of the script is halted until user clicks Open or Cancel on the dialog box screen. Won't go to the next line of the script.
Wondering if anybody had this issue or is it unique to attaching a file to a work order through "Services for Object" functionality.
Regards
Umur
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.