cancel
Showing results for 
Search instead for 
Did you mean: 

Closing SAP Window via VB.net

Former Member
0 Kudos

Hello,

I am developing a console app in VB.net which will run automatically using windows task schedule service. The program will extract information from SAP GUI around 10 times at day, so I am trying to find a way to close the SAP Windows once the program is terminated otherwise I will have hundred of SAP windows opened because there will not be a user in the computer where the program will run.

My connection script is as follows:

Dim SapGuiAuto = GetObject("SAPGUI")
        Dim application = SapGuiAuto.GetScriptingEngine
        Dim connection = application.OpenConnection("system", True, False)
        Dim MySession = connection.Children.Item(0)


        MySession.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "user"
        MySession.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = "password"
        MySession.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "E"
        MySession.findById("wnd[0]").sendVKey(0)

  MySession.findById("wnd[0]/tbar[0]/okcd").Text = "/nzdown1"

        MySession.findById("wnd[0]").sendVKey(0)

        MySession.findById("wnd[0]/usr/btn%_SMATNR_%_APP_%-VALU_PUSH").press()

        MySession.findById("wnd[1]/tbar[0]/btn[24]").press()

        MySession.findById("wnd[1]").sendVKey(8)

        MySession.findById("wnd[0]/usr/chkP_LOC1").Selected = False

        MySession.findById("wnd[0]/usr/chkP_LOC2").Selected = False

        MySession.findById("wnd[0]/usr/chkP_LOC3").Selected = False

        MySession.findById("wnd[0]/usr/chkP_LOC3").SetFocus()

        MySession.findById("wnd[0]").sendVKey(8)

        MySession.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").Text = "c:\class.txt"

        MySession.findById("wnd[1]/usr/ctxtRLGRAP-FILENAME").caretPosition = 12

        MySession.findById("wnd[1]").sendVKey(0)

' HERE IS  WHERE I WANT TO CLOSE THE SAP WINDOWS

    MySession = Nothing
        connection = Nothing
        application = Nothing
        SapGuiAuto = Nothing

    End Sub

Accepted Solutions (1)

Accepted Solutions (1)

script_man
Active Contributor
0 Kudos

Hi Magda,

welcome to the forum. You can try the following:

. . .

while connection.children.count > 0

   Set MySession    = connection.Children(0)

   MySession.findbyid("wnd[0]").close

   on error resume next

   MySession.findById("wnd[1]/usr/btnSPOP-OPTION1").press

   on error goto 0

wend

This variant of VBS-Code terminates all open SAP sessions.

Regards,

ScriptMan

Former Member
0 Kudos

You are the best!!! Yep this is what I was looking for

Thank you so much.

ironlungs602
Member
0 Kudos

Just an FYI to shorten it further without error handling manipulation.

Instead of:

on error resume next

MySession.findById("wnd[1]/usr/btnSPOP-OPTION1").press

on error goto 0

Use:

if not MySession.findById("wnd[1]/usr/btnSPOP-OPTION1", false) is nothing then MySession.findById("wnd[1]/usr/btnSPOP-OPTION1").press

Answers (0)