cancel
Showing results for 
Search instead for 
Did you mean: 

Script recording doesn't record save of file

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (4)

Answers (4)

0 Kudos

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

0 Kudos

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.

0 Kudos

Hi,

uncheck the "Show native MS Windows dialogs" option in the SAPGUI options (see screenhot), then it will work.

All the best

Martin

holger_khn
Contributor
0 Kudos

Hi Martin.

If they are ot on SAP_BASIS 740 this is not an Option.

0 Kudos

Hi Holger,

correct me if I'm wrong, but I belive this depends on the SAP GUI version, not the SAP_BASIS version. It's true that you need SAPGUI 7.40, though.

Shouldn't be too hard to upgrade that one, though ;-.)

All the best

Martin

Former Member
0 Kudos

You are right 😞

This is not an option for me. I'm using GUI 720

holger_khn
Contributor
0 Kudos

There are some more Options. One is to use API-functions checking in a Loop for SAP MS SaveAS Dialog in a seperate Excel instance. In this Forum and many other places you will find a lot of examples how to use.

This is done via some USER32.DLL API functions.

gregorw
Active Contributor
0 Kudos

Hi A,

perhaps http://www.autoitscript.com/autoit3/index.shtml can help you there?

Best regards

Gregor

script_man
Active Contributor
0 Kudos

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

Former Member
0 Kudos

Hi Scriptman,

The script works partly when I copy and paste it from the forum. The filename isn't filled in the "Save As" box. When I fill a value manually the filled is saved directly becasue of the loop.

script_man
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

script_man
Active Contributor
0 Kudos

Hi,

Which character is underlined in "File Name" box?

Is it an "F" then it should read:

wscript.sendkeys "%f"

instead

wscript.sendkeys "%n"

If this works, we need the tabs not.

Regards,

ScriptMan

Edited by: ScriptMan on Sep 30, 2009 9:23 AM

Former Member
0 Kudos

Hi,

When i click the dropdown box from "File Name" (displays historical filenames) multiple times the file name given at Path is displayed and the file is saved.

script_man
Active Contributor
0 Kudos

Hi,

I just wanted to suggest that one could use hotkeys. If you can't use it, also helps

wscript.sendkeys "{TAB 4}"

Regards,

ScriptMan

Former Member
0 Kudos

Thanks for your help.

Former Member
0 Kudos

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

Former Member
0 Kudos

I have the same problem 😞

Former Member
0 Kudos

Hi script Man,

Is there anything similar for attaching the file ?