Você está na página 1de 2

Global Session

Sub Conexao_SAP(ByVal Transacao As String)

'Abre sistema SAP até a tela de inserção da transação


Dim Connection, SapGui, Appl, WshShell, proc
Dim mensagem As String

On Error GoTo DESLOGADO


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

DESLOGADO:

Set WshShell = CreateObject("WScript.Shell")

Set proc = WshShell.Exec("C:\Program Files (x86)\SAP\FrontEnd\SAPgui\


saplogon.exe")

Application.Wait Now + TimeValue("0:00:5")

Set SapGui = GetObject("SAPGUI")

Set Appl = SapGui.GetScriptingEngine

Set Connection = Appl.OpenConnection("01 [BRF] - SAP ECC Produção -->Logon


Automático<---", True)

Set Session = Connection.Children(0)

LOGADO:
On Error GoTo -1

Application.DisplayAlerts = False
On Error Resume Next
If Not IsObject(ApplicationSAP) Then
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
If Err Then MsgBox ("Sistema SAP - Não encontrado..!")
Set ApplicationSAP = SapGuiAuto.GetScriptingEngine
If Err Then MsgBox ("Sistema SAP - Cancelado..!")
End If

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

mensagem = .findById("wnd[0]/sbar").Text

If mensagem <> Empty Then


Application.StatusBar = mensagem
End If
End With

End Sub

Você também pode gostar