Home > other >  Full Code SAP Connection in VBA - Help on Error 424 Object Required
Full Code SAP Connection in VBA - Help on Error 424 Object Required

Time:06-06

I'm new to VBA and coding in general, I was able to edit this code and make VBA connect with SAP. But when I make the call the code stops working on the first line of second code - "Session.findById..." and gives the error "Run time error 424: Object Required".

Follow the codes and the error occurs on the commented line in the second code.

Full code of the SAP connection module:

Global Session

Sub SAP_Login(ByVal Transacao As String)

Dim SapGui
Dim Applic
Dim Connection
Dim Session
Dim WshShell
Dim mensagem As String

On Error GoTo DESLOGADO
    Set Session = GetObject("SAPGUI").GetScriptingEngine.Children(0).Children(0)
    
GoTo LOGADO

DESLOGADO:

'Abre o Sap instalado na sua máquina
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus

'inicia a variável com o objeto SAP
Set WshShell = CreateObject("WScript.Shell")

Do Until WshShell.AppActivate("SAP Logon ")
    Application.Wait Now   TimeValue("0:00:01")
Loop

Set WshShell = Nothing

Set SapGui = GetObject("SAPGUI")

Set Applic = SapGui.GetScriptingEngine

Set Connection = Applic.OpenConnection("NEA Dow Prod - ERP Central Component (ECC) (SSO) (001)", True)

Set Session = Connection.Children(0)

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

'DADOS PARA FAZER O LOGIN NO SISTEMA
'On Error Resume Next
'Session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "800" 'client do sistema

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

LOGADO:

With Session
    .findById("wnd[0]/tbar[0]/okcd").Text = "/o"
    .findById("wnd[0]").sendVKey 0
    .findById("wnd[0]/tbar[0]/okcd").Text = "/n"
    .findById("wnd[0]").sendVKey 0
    .findById("wnd[0]/tbar[0]/okcd").Text = Transacao
    .findById("wnd[0]").sendVKey 0
    
'possivel texto de erro sap vai aparecer no excel
      mensagem = .findById("wnd[0]/sbar").Text
      
      If mensagem <> Empty Then
         Application.StatusBar = mensagem
      End If
End With

'ESSA PARTE SAI DO SISTEMA POS A EXECUÇÃO DO CÓDIGO

'Set session = Nothing
'Application.Wait Now   TimeValue("0:00:05")
'connection.CloseSession ("ses[0]")
'Set connection = Nothing
'Set sap = Nothing


End Sub

Second code that makes the call and continues the SAP script (it's on a button):

Private Sub EXTRAIR_Click()

Call SAP_Login("coois")
    
    
    Session.findById("wnd[0]/mbar/menu[2]/menu[0]/menu[0]").Select 'THIS IS WHERE THE ERROR BEGINS!!!
    Session.findById("wnd[1]/usr/txtENAME-LOW").Text = "nd87309"
    Session.findById("wnd[1]").sendVKey 8
    Session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_ECKST-LOW").Text = "07062022"
    Session.findById("wnd[0]/usr/tabsTABSTRIP_SELBLOCK/tabpSEL_00/ssub%_SUBSCREEN_SELBLOCK:PPIO_ENTRY:1200/ctxtS_ECKST-HIGH").Text = "07062022"
    Session.findById("wnd[0]").sendVKey 8
    Session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").SetCurrentCell -1, ""
    Session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").SelectAll
    Session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell").ContextMenu
    Session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").SelectContextMenuItemByPosition "0"
    
    Range("A2").PasteSpecial
    MsgBox "Copiado com Sucesso"

End Sub

CodePudding user response:

You have

Dim Session

inside SAP_Login: that variable will "hide" the Global variable with the same name, so when that sub exits the Global is still Nothing. Remove the local variable declaration so only the Global one remains.

  • Related