SAP GUI DOES NOT SAVE in MACRO VBA

szymon94

New Member
Joined
Nov 14, 2022
Messages
20
Office Version
  1. 365
  2. 2021
  3. 2019
Platform
  1. Windows
Hello I have a problem, Ihave recorded a script that I want to use in excel vba but somehow it does not recording saving. Is there any different way to write the code that would save me the reports ?

Sub WCARG60()
Dim SapGuiAuto, Application, Connection, Session
Dim path As String 'add a variable for the file path
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

Session.findById("wnd[0]").Maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "Z_L_RU_OPS_TRN_MRX"
Session.findById("wnd[0]").sendVKey 0
Session.findById("wnd[0]/usr/radP_RB_A").Select
Session.findById("wnd[0]/usr/radP_RB_WRK").Select
Session.findById("wnd[0]/usr/txtS_FRGGR-LOW").Text = "60"
Session.findById("wnd[0]/usr/ctxtS_FRGCO-LOW").Text = "00"
Session.findById("wnd[0]/usr/ctxtS_FRGCO-HIGH").Text = "zz"
Session.findById("wnd[0]/usr/radP_RB_WRK").SetFocus
Session.findById("wnd[0]/tbar[1]/btn[8]").press
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell 7, "FRGCO"
Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "7"
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

Aftet the Press dialog box saves as is open but it does not save

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
To save the file after the "Save As" dialog box is open, you can use the Session.findById method to interact with the dialog box and set the file path and name. Here's an example of how you can modify your code to save the file:

VBA Code:
Sub WCARG60()
    Dim SapGuiAuto As Object, Application As Object, Connection As Object, Session As Object
    Dim path As String ' add a variable for the file path
    
    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

    Session.findById("wnd[0]").Maximize
    Session.findById("wnd[0]/tbar[0]/okcd").Text = "Z_L_RU_OPS_TRN_MRX"
    Session.findById("wnd[0]").sendVKey 0
    Session.findById("wnd[0]/usr/radP_RB_A").Select
    Session.findById("wnd[0]/usr/radP_RB_WRK").Select
    Session.findById("wnd[0]/usr/txtS_FRGGR-LOW").Text = "60"
    Session.findById("wnd[0]/usr/ctxtS_FRGCO-LOW").Text = "00"
    Session.findById("wnd[0]/usr/ctxtS_FRGCO-HIGH").Text = "zz"
    Session.findById("wnd[0]/usr/radP_RB_WRK").SetFocus
    Session.findById("wnd[0]/tbar[1]/btn[8]").press
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").setCurrentCell 7, "FRGCO"
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectedRows = "7"
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
    
    ' Wait for the "Save As" dialog box
    Session.findById("wnd[1]").WaitFor("Name", "wnd[1]", 5000)
    
    ' Set the file path and name in the "Save As" dialog box
    path = "C:\Path\to\save\the\file.xlsx" ' Replace with your desired file path and name
    Session.findById("wnd[1]/usr/ctxtDY_PATH").Text = path
    Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = "file.xlsx" ' Replace with your desired file name
    
    ' Click the "Save" button in the "Save As" dialog box
    Session.findById("wnd[1]/tbar[0]/btn[0]").press
End Sub

Make sure to replace "C:\Path\to\save\the\file.xlsx" with the desired file path and name where you want to save the file.

This modified code sets the file path and name in the "Save As" dialog box using the Session.findById method and then clicks the "Save" button to save the file.
 
Upvote 0

Forum statistics

Threads
1,223,782
Messages
6,174,520
Members
452,569
Latest member
Ron1970

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top