cancel
Showing results for 
Search instead for 
Did you mean: 

Opening SAP using macro

Former Member
0 Kudos

Hi everyone,

with the code i was able to open a connection to SAP .

Set sap = CreateObject("SapAutoGui.Event")

      host = "test12"
    sapNr = "90"
    mandant = "800"
    user =" test1"
    Password = "pass"
      
 
    bOK = sap.Connect(host , sapNr ,SapGuiActiveX Or SapGuiFront Or SapGuiFullMenu)
    OKCheck bOK, "Error in opening connection"
    
    iCtrl = sap.FindByField("RSYST-MANDT", 0, SapEdit)
    bOK = sap.SetControlValue(iCtrl, mandant)
    OKCheck bOK, "Error in setting text value"

    iCtrl = sap.FindByField("RSYST-BNAME", 0, SapEdit)
    bOK = sap.SetControlValue(iCtrl, user )
    OKCheck bOK, "Error in setting text value"

    iCtrl = sap.FindByField("RSYST-BCODE", 0, SapPassword)
    
    bOK = sap.SetControlValue(iCtrl, Password)
    OKCheck bOK, "Error in setting text value"

    bOK = sap.SetCursorByControl(iCtrl)
    OKCheck bOK, "Error in setting cursor position"

    bOK = sap.SendEvent
    OKCheck bOK, "Error in sending default key"

but since SNC is active, i can't open sap

how do I make a connection to SAP  when SNC = true

Can anyone help me?


Thanks,

saad

Accepted Solutions (1)

Accepted Solutions (1)

holger_khn
Contributor
0 Kudos

Hello Saad,

'--------------------------Start Excel VBA Sub-----------------------------

Sub SAP_OpenSessionFromLogon()
Dim SapGui
Dim Applic
Dim connection
Dim session

Shell ("C:\Program Files\SAP\FrontEnd\SAPgui\saplogon.exe")

Set SapGui = GetObject("SAPGUI")

Rem Create the GuiApplication object
Set Applic = SapGui.GetScriptingEngine

Rem Open a connection
Set connection = Applic.OpenConnection("QE1 - ERP QA ECC 6.0", True) '<=== here you need to fillin your connection description

Set session = connection.Children(0)

session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800"
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "Test1"
session.findById("wnd[0]").sendVKey 0

Rem Do something: Either fill out the login screen
Rem or in case of Single-Sign-On start a transaction.
session.SendCommand ("/nbibs")

MsgBox "Waiting..."

Rem Shutdown the connection
Set session = Nothing
connection.CloseSession ("ses[0]")
Set connection = Nothing
Set sap = Nothing

MsgBox "Done"

End Sub

'--------------------------End of Excel VBA Sub----------------------------

Just fillin the description from your system in row where 'Set connection' is defined.

If you have any questions let me know. Br, Holger

Former Member
0 Kudos

I'll try it
  thank you

Former Member
0 Kudos

I have tried the code.

it appeared  the message "The control Could not found by id".

session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800"

I have  added the command "on error resume next"  and it has finaly worked.

Vielen DANKE!!!!!!!!!!!

Is there any documentation on this subject?

holger_khn
Contributor
0 Kudos

Hello.

Check for SAPGuiScripting helpfile in any searchmachine and download.

When you have only one client (client = "800") on this system this lines are not required.

Or you can check if they´re changeable

session.findById("wnd[0]").maximize

If session.findById("wnd[0]/usr/txtRSYST-MANDT").Changeable = True Then

session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800"

session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = "Test1"

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

End If

Br, Holger

Former Member
0 Kudos

Hello Holger,

it works now

Thank you very much

Former Member
0 Kudos

Hello ,

the code has worked fine under xp.
Now I have windows 7 and the code no longer works.
can someone help me

holger_khn
Contributor
0 Kudos

This code is working in all Windows OS. So Win7 will not be the root cause for your issues. Without being more detailed I suggest you check your local settings as this seems like root cause.

Former Member
0 Kudos

Hello Holger,

I have made the following settings
-  User Settings Enable
- Accessibility mode   enabeld
- classic design        enabeld
-Activate  GUIXT

did i forget something?

Thanks!!!!!!!!!

Former Member
0 Kudos

Hello Holger,

Thank you for your help.

I appreciate you can help to connect SAP using SNC Name / Server, instead of SAP Description on System Connection parameters.

Is it possible?. Thank you.-

former_member213011
Participant
0 Kudos

Hello Javier,

In Holger's codes, you can replace the set connection = Applic.OpenConnection("your.connection.name", True) to set connection = Applic.OpenConnectionByConnectionString("your.server.name.here", True).

A more proper way to use the OpenConnectionByConnectionString method is to use the prefixes /H/ for 'Host', /S/ for 'Service (port)', /M/ for 'Message Server' where applicable, but I found that discarding the prefixes still works.

Thanks,

Sayuti

Former Member
0 Kudos

Hello Sayuti,

Tks for your prompt answer.

I tried with Server Name, but it is opening a SAP section for UserID and Password. in our Co we have dual mode (User/Password) or SNC (UserID Only), for the same Server Name.

I'll use description name (picked by SAPGUI client) and logon using that parameters.

Thank you for your help.-

former_member213011
Participant
0 Kudos

Hi Javier,

Sorry can't offer further help with this issue as I'm not familiar with the dual mode concept.

Thanks,

Sayuti

Answers (0)