cancel
Showing results for 
Search instead for 
Did you mean: 

Script recording dosen't save file path

Former Member
0 Kudos

I Guy's, i use sapGui 7.40 and i try to make an automatic script for exctract data by a query in sq01.

Everything work correctly but i can't save the file path and name.

This is my example script :

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 = "sq01"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/mbar/menu[5]/menu[0]").select
session.findById("wnd[1]/usr/radRAD1").select
session.findById("wnd[1]/tbar[0]/btn[2]").press
session.findById("wnd[0]/tbar[1]/btn[19]").press
session.findById("wnd[1]/usr/cntlGRID1/shellcont/shell").currentCellRow = 28
session.findById("wnd[1]/usr/cntlGRID1/shellcont/shell").firstVisibleRow = 18
session.findById("wnd[1]/usr/cntlGRID1/shellcont/shell").selectedRows = "28"
session.findById("wnd[1]/usr/cntlGRID1/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").selectedRows = "0"
session.findById("wnd[0]/usr/cntlGRID_CONT0050/shellcont/shell").doubleClickCurrentCell
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/ctxtSP$00005-LOW").text = "zpf"
session.findById("wnd[0]/usr/ctxtSP$00006-LOW").text = "mp01"
session.findById("wnd[0]/usr/ctxtSP$00006-LOW").setFocus
session.findById("wnd[0]/usr/ctxtSP$00006-LOW").caretPosition = 4
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.findById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
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

Can anyone help me?

Accepted Solutions (0)

Answers (4)

Answers (4)

kezsmarkia
Explorer
0 Kudos

Hi Stefan,

For some reasons the above macro does not work properly for me. Could you please advise?

This is the part of the code for the export:

'Export to spreadsheet - first 2 lines are recorded when I do the export with recording, and when running it works well, opens the non-scriptable "Save As" window

session.FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT" session.FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").SelectContextMenuItem "&XXL"

'Start asynchronous process

Set WshShell = CreateObject("WScript.Shell") WshShell.Run "C:\Users\AAA.LOGON\Documents\AAA\ScriptCA.vbs"

'-Begin-----------------------------------------------------------------

Set wshShell = CreateObject("WScript.Shell")

Do

WScript.Sleep 500

Loop Until wshShell.AppActivate("Save As") = True

wshShell.Sendkeys "%nNA_SQ00FBL5N_CA10.XML"

'wshShell.Sendkeys "%nNA_SQ00FBL5N_CA10.XML%f"

'-End-------------------------------------------------------------------

Issue 1 is that I do not know how to get "Save" button pressed by the macro, your suggestion did not work for me.

Issue 2 is that I run 3 queries within the same macro and I would like to export all 3 files in XML format with different names and I have 3 scripts referenced. When running the macro file names are filled randomly, not according to the scripts. It seems to me that the first file name is filled twice and then the second comes instead of the third and then would the third come if I had a fourth query.

stefan_schnell
Active Contributor
0 Kudos

Hello kezsmarkia,

it is not necessary two ask your question twice.

https://answers.sap.com/questions/13068673/sap-spreadsheet-export-with-non-scriptable-window.html

Best regards
Stefan

Former Member
0 Kudos

Thanks Stefan

i have 2 question's :

1- your example are write on a sapscript or inside a Excel macro?

2 - fillfilename.vbs is your last code?

this is fillfilename?

'-Begin-----------------------------------------------------------------
Set wshShell = CreateObject("WScript.Shell")
Do
WScript.Sleep 500
Loop Until wshShell.AppActivate("Datei importieren") = True
wshShell.Sendkeys "%nTest.txt"
'wshShell.Sendkeys "%nTest.txt%f"
'-End-------------------------------------------------------------------

if i try your first example i have problem on this "session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Test.txt""

and i dont' know where i have to put it in my script.

thanks

stefan_schnell
Active Contributor
0 Kudos

Hello Roberto,

sorry for my delayed reply.

To your questions:

  1. It is VBA in Excel.
  2. No, the last line is
    session.FindById("wnd[0]/titl/shellcont/shell").SelectContextMenuItem "%GOS_PCATTA_CREA"
    fillfilename starts a parallel task
    Yes, this is fillfilename.
    If your GOS uses a scriptable dialog you can use the first example, otherwise use the second.

Let us know your results.

Cheers
Stefan

Former Member
0 Kudos

Thanks Stefan. The option in dialog box i already uncheck.

you know if is it possibile to do the operation by script or i need:

Excel VBA --> call sap transaction ---> set name file an path to save?

Do you have some example? at your link i see print or import file. I can exctract but automatically set a path.

Sorry for my bad english

stefan_schnell
Active Contributor
0 Kudos

Hello Roberto,

here an example how to use the GOS

Sub Test1()

  Dim SapGuiAuto As Object
  Dim App As SAPFEWSELib.GuiApplication
  Dim connection As SAPFEWSELib.GuiConnection
  Dim session As SAPFEWSELib.GuiSession
    
  Set SapGuiAuto = GetObject("SAPGUI")
  Set App = SapGuiAuto.GetScriptingEngine
  Set connection = App.Children(0)
  Set session = connection.Children(3)

  session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nsgostest"
  session.FindById("wnd[0]").SendVKey 0
  session.FindById("wnd[0]/tbar[1]/btn[8]").Press
  session.FindById("wnd[0]/titl/shellcont/shell").PressContextButton "%GOS_TOOLBOX"
  session.FindById("wnd[0]/titl/shellcont/shell").SelectContextMenuItem "%GOS_PCATTA_CREA"
  session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Test.txt"

End Sub

In my case I get a scriptable dialog to import a file.

And here an example with a native Windows dialog.

Sub Test2()

  Dim SapGuiAuto As Object
  Dim App As SAPFEWSELib.GuiApplication
  Dim connection As SAPFEWSELib.GuiConnection
  Dim session As SAPFEWSELib.GuiSession
    
  Set SapGuiAuto = GetObject("SAPGUI")
  Set App = SapGuiAuto.GetScriptingEngine
  Set connection = App.Children(0)
  Set session = connection.Children(3)

  session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nsgostest"
  session.FindById("wnd[0]").SendVKey 0
  session.FindById("wnd[0]/tbar[1]/btn[8]").Press
  session.FindById("wnd[0]/titl/shellcont/shell").PressContextButton "%GOS_TOOLBOX"
  
  '-Start asynchronous process-
  Dim WshShell As Object
  Set WshShell = CreateObject("WScript.Shell")
  WshShell.Run "C:\Dummy\FillFileName.vbs"
  
  session.FindById("wnd[0]/titl/shellcont/shell").SelectContextMenuItem "%GOS_PCATTA_CREA"

End Sub

As you can see I start, before I open the native dialog, a asynchronous VB script to fill the dialog.

'-Begin-----------------------------------------------------------------

Set wshShell = CreateObject("WScript.Shell")

Do
  WScript.Sleep 500
Loop Until wshShell.AppActivate("Datei importieren") = True

wshShell.Sendkeys "%nTest.txt"
'wshShell.Sendkeys "%nTest.txt%f"

'-End-------------------------------------------------------------------

As you can see AppActivate searches for the title "Datei importieren" (German for File Import) and Sendkeys types Alt+n (this means the underscore of Dateiname (Filename)) and the filename Test.txt. The commented line presses also Alt+f (this means the underscore of Öffnen (Open)) to close the dialog.

After that your VBA code runs further after %GOS_PCATTA_CREA.

Let us know your results.
Cheers
Stefan

stefan_schnell
Active Contributor
0 Kudos

Hello Roberto,

SAP GUI Scripting doesn't handle native Windows dialogs.

At first be sure that the native dialogs option in the SAP Logon is disabled.

If it is disabled you can use AutoIt or you need tricky VBScript mechanisms to handle native dialogs. You can find different examples here and here.

Let us know your results.

Cheers
Stefan

former_member792236
Discoverer
0 Kudos

Hi Stefan,

I have a question related to Import File to SAP.

I've done the instruction you suggested (below). I get the pasted file name in the Import file window.

However, finally I receive the error:

"Error occurred during the import"

The import was not carried out.

Do you possibly know any hints?

Thanks in advance

T.

session.FindById("wnd[0]/titl/shellcont/shell").PressContextButton "%GOS_TOOLBOX"

  
  '-Start asynchronous process-
  Dim WshShell As Object
  Set WshShell = CreateObject("WScript.Shell")
  WshShell.Run "C:\Dummy\FillFileName.vbs"
  
  session.FindById("wnd[0]/titl/shellcont/shell").SelectContextMenuItem "%GOS_PCATTA_CREA"

End Sub
'-Begin-----------------------------------------------------------------

Set wshShell = CreateObject("WScript.Shell")

Do
  WScript.Sleep 500
Loop Until wshShell.AppActivate("Datei importieren") = True

wshShell.Sendkeys "%nTest.txt"
'wshShell.Sendkeys "%nTest.txt%f"

'-End-------------------------------------------------------------------
jerryjanda
Community Manager
Community Manager

Please don't ask a new question -- posted as an answer -- in an old thread. You won't reach anybody this way. You should ask a new question instead: https://answers.sap.com/questions/ask.html/ I recommend that you familiarize yourself with https://community.sap.com/resources/questions-and-answers, as it provides tips for preparing questions that draw responses from our members. Feel free to take our Q&A tutorial at https://developers.sap.com/tutorials/community-qa.html as well, as that will also help you when preparing questions for the community.

Kind regards,

--Jerry

stefan_schnell
Active Contributor

tom_asz

Hello Thomasz,
please follow the advice of the administrator.

I assume you got the message SO424. Make sure that the document is closed and not opened by any application. You can find more information here. Or the MIME, maintained in table MIMETYPES, is for a different purpose, you can find more information at OSS note 2599578. Maybe there are more reasons.

Best regards
Stefan