on 09-28-2009 1:27 PM
Hi All,
I'm new with script recording. I made a recording of the generation of an HTML file of an SQ02 info set.
I want to save the HTML file automatically. At the moment the last step is the opening of the "Save As" screen in the sripting. A dynamically name must be given and the file must be saved. Because the recording doesn't record the actual saving of the file I'm stuck. I hope some one can help me with the scripting code, or has an example code. Thanks in advance. The code is listed 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 = "/nse38"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRS38M-PROGRAMM").text = "RSAQSHSG"
session.findById("wnd[0]/usr/ctxtRS38M-PROGRAMM").caretPosition = 8
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/usr/txtPA_SG").text = "ZTARIFFS"
session.findById("wnd[0]/usr/txtPA_MODE").text = "G"
session.findById("wnd[0]/usr/txtPA_MODE").setFocus
session.findById("wnd[0]/usr/txtPA_MODE").caretPosition = 1
session.findById("wnd[0]").sendVKey 8
session.findById("wnd[0]/tbar[1]/btn[16]").press
TO : ALL EXPERTS
I have the sample problem and stuck over half year
Because my knowledge in VBA is almost nothing.
After reading some Q&A, I still can't understand how to work. My SAP is GUI 740 with Win 10.
Any Expert can help me to write a simple VBA for me which continuous my job on below and save the export file into "C:\temp\".
Mega thanks in advance.
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]").resizeWorkingPane 79,19,false
session.findById("wnd[0]/tbar[0]/okcd").text = "FAGLL03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/radX_AISEL").select
session.findById("wnd[0]/usr/ctxtSD_SAKNR-LOW").text = "6511042000"
session.findById("wnd[0]/usr/ctxtSD_SAKNR-HIGH").text = "6579042902"
session.findById("wnd[0]/usr/ctxtSD_BUKRS-LOW").text = "4200"
session.findById("wnd[0]/usr/ctxtSO_BUDAT-LOW").text = "20220501"
session.findById("wnd[0]/usr/ctxtSO_BUDAT-HIGH").text = "20220531"
session.findById("wnd[0]/usr/radX_AISEL").setFocus
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").select
session.findById("wnd[1]/usr/radRB_OTHERS").setFocus
session.findById("wnd[1]/usr/radRB_OTHERS").select
session.findById("wnd[1]/usr/cmbG_LISTBOX").key = "10"
session.findById("wnd[1]/tbar[0]/btn[0]").press
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello
im in 7.60, and the above option not works for me.
But if i choose to export as local file instead spreadsheet, the dialog box is a SAP dialog and the script toll record it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi A.,
the link from Gregor is very interesting. But if it should go quickly, I would try the following:
...
session.findById("wnd[0]").sendVKey 8
Path = inputbox("Please enter a filename: ","filename","c:\tmp\test.htm")
set Wshell = CreateObject("WScript.Shell")
wshell.run """c:\tmp\save_as.vbs """ & Path
session.findById("wnd[0]/tbar[1]/btn[16]").press
Save_as.vbs includes:
if Wscript.Arguments.count > 0 then
set fs = CreateObject("Scripting.FileSystemObject")
if fs.fileExists(WScript.arguments(0)) then
Set myfile = fs.GetFile(WScript.arguments(0))
myfile.Delete
end if
set Wshell = CreateObject("WScript.Shell")
Do
bWindowFound = Wshell.AppActivate("Save As")
WScript.Sleep 1000
Loop Until bWindowFound
Do
Wshell.appActivate "Save As"
Wshell.sendkeys WScript.arguments(0)
WScript.Sleep 400
Wshell.appActivate "Save As"
Wshell.sendkeys "%s"
WScript.Sleep 400
bWindowFound = Wshell.AppActivate("Save As")
WScript.Sleep 400
Loop Until not bWindowFound
end if
Regards,
ScriptMan
Hi A.,
where is the cursor when the box is opened? If he is not in the Filename field, it can be placed there.
For example:
...
Do
Wshell.appActivate "Save As"
Wshell.sendkeys "%n"
WScript.Sleep 400
Wshell.appActivate "Save As"
Wshell.sendkeys WScript.arguments(0)
WScript.Sleep 400
Wshell.appActivate "Save As"
Wshell.sendkeys "%s"
WScript.Sleep 400
bWindowFound = Wshell.AppActivate("Save As")
WScript.Sleep 400
Loop Until not bWindowFound
end if
Regards,
ScriptMan
Edited by: ScriptMan on Sep 29, 2009 4:12 PM
Hi,
The cursor is blinking in the 'File Name' box. But the 'Save In' field is blinking blue. I changed the code with:
if Wscript.Arguments.count > 0 then
set fs = CreateObject("Scripting.FileSystemObject")
if fs.fileExists(WScript.arguments(0)) then
Set myfile = fs.GetFile(WScript.arguments(0))
myfile.Delete
end if
set Wshell = CreateObject("WScript.Shell")
Do
bWindowFound = Wshell.AppActivate("Save As")
WScript.Sleep 1000
Loop Until bWindowFound
Do
Wshell.sendkeys "{TAB}"
Wshell.sendkeys "{TAB}"
Wshell.sendkeys "{TAB}"
Wshell.sendkeys "{TAB}"
Wshell.appActivate "Save As"
Wshell.sendkeys WScript.arguments(0)
WScript.Sleep 400
Wshell.appActivate "Save As"
Wshell.sendkeys "%s"
WScript.Sleep 400
bWindowFound = Wshell.AppActivate("Save As")
WScript.Sleep 400
Loop Until not bWindowFound
end if
I added 4 tabs. It may not be the most ellegant solution but it works.
Edited by: A. van Dommelen on Sep 30, 2009 9:10 AM
Hi ScriptMan,
I tried to copy your code into my VBA .
But after above window shows up, my code stop running.
All I need is in this step VBA can helps me to click "Save" buttom. and then in my SAP, "export.xlsx" will open automatically, and then I can handle rest work.
Option Explicit
Public SapGuiAuto As Object, Applicat As Object, Connection As Object, Session As Object
Function Attach() As Boolean
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
On Error GoTo 0
If SapGuiAuto Is Nothing Then
Attach = False
Exit Function
Else
Set Applicat = SapGuiAuto.GetScriptingEngine
On Error GoTo 0
End If
If Applicat Is Nothing Then
Attach = False
Exit Function
End If
If Applicat.Children.Count = 0 Then
Attach = False
Exit Function
Else
Set Connection = Applicat.Children(0)
On Error GoTo 0
End If
Set Session = Connection.Children(0)
If Session.ActiveWindow.Text = "SAP" Then
Attach = False
Exit Function
End If
Attach = True
End Function
Sub savExcl()
If Attach Then
''below code is SAP
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmb52"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").currentCellColumn = "LABST"
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "0"
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
Session.findById("wnd[1]/tbar[0]/btn[0]").press
''SAP running over
Dim wscript, wshell, bWindowFound
Dim fs, myfile
If wscript.arguments.Count > 0 Then
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.fileExists(wscript.arguments(0)) Then
Set myfile = fs.GetFile(wscript.arguments(0))
myfile.Delete
End If
Set wshell = CreateObject("WScript.Shell")
Do
bWindowFound = wshell.AppActivate("Save As")
wscript.Sleep 1000
Loop Until bWindowFound
Do
wshell.AppActivate "Save As"
wshell.SendKeys wscript.arguments(0)
wscript.Sleep 400
wshell.AppActivate "Save As"
wshell.SendKeys "%s"
wscript.Sleep 400
bWindowFound = wshell.AppActivate("Save As")
wscript.Sleep 400
Loop Until Not bWindowFound
End If
End If 'respond to attach
End Sub
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.