-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsap.cls
145 lines (112 loc) · 4.16 KB
/
sap.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "SapGuiScripting"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' =========================================================================================='
' Author: Henrique Campiotti (ehnraem) '
' Description: Simple class module to use SAP GUI Scripting API in a easy way '
' Documentation: https://help.sap.com/viewer/b47d018c3b9b45e897faf66a6c0885a8/760.00/en-US '
' =========================================================================================='
Option Explicit
Private Declare PtrSafe Function CoRegisterMessageFilter Lib "ole32.dll" (ByVal IFilterIn As Long, ByRef PreviousFilter As Long) As Long
Private Const mTab = " "
Private gui As Object
Private app As GuiApplication
Private con As GuiConnection
Public session As GuiSession
Private Sub Class_Terminate()
If Not C1ose() Then Application.StatusBar = "SAP Connection was not closed"
End Sub
Public Function Attach( _
profile As String, _
Optional ByVal inplace As Boolean = False, _
Optional ByVal usr As String, _
Optional ByVal pwd As String _
) As Boolean
' Attach to a running instance of SAP GUI (getting the object)
On Error Resume Next
Set gui = GetObject("SAPGUI")
On Error GoTo 0
If gui Is Nothing Then
Application.StatusBar = "[ ! ] SAP Logon instance was not found"
Attach = False
Exit Function
End If
' Getting the scripting application
Set app = gui.GetScriptingEngine
Attach = CreateConnection(profile, inplace, usr, pwd)
End Function
Private Function CreateConnection( _
profile As String, _
inplace As Boolean, _
Optional usr As String, _
Optional pwd As String _
) As Boolean
' To create a new SAP GUI instance placed within your application
Dim inplSuffix$: If inplace Then inplSuffix = "/INPLACE"
On Error Resume Next
Set con = app.OpenConnection(profile & inplSuffix, True, False)
On Error GoTo 0
If con Is Nothing Then
Application.StatusBar = "[ ! ] Open Connection fail"
Set app = Nothing
Exit Function
End If
Set session = con.Sessions(0)
If Not Logon(usr, pwd) Then Exit Function
Call MultipleLogon(session)
session.ActiveWindow.Maximize
Application.StatusBar = session.info.User & mTab & session.info.SystemName & mTab & _
session.info.Client & mTab & session.Name
Call CoRegisterMessageFilter(0, 0)
CreateConnection = True
End Function
Private Function Logon(usr As String, pwd As String) As Boolean
On Error Resume Next
Dim usrField As GuiTextField, pwdField As GuiPasswordField
With session.ActiveWindow
Set usrField = .FindById("usr/txtRSYST-BNAME")
Set pwdField = .FindById("usr/pwdRSYST-BCODE")
End With
On Error GoTo 0
If usrField Is Nothing And pwdField Is Nothing Then
Application.StatusBar = "[ + ] Single sign-on active"
Logon = True
Exit Function
End If
usrField.text = usr
pwdField.text = pwd
session.ActiveWindow.SendVKey 0
Dim sts As GuiStatusbar
Set sts = session.ActiveWindow.FindById("sbar")
If sts.MessageType = "E" Then
Application.StatusBar = sts.text
Exit Function
End If
Logon = True
End Function
Private Sub MultipleLogon(session As GuiSession)
While session.Children.Count > 1:
On Error Resume Next
session.FindById("wnd[1]/usr/radMULTI_LOGON_OPT2").Select
session.ActiveWindow.SendVKey 0
On Error GoTo 0
Wend
End Sub
Private Function C1ose() As Boolean
On Error GoTo eh:
If con Is Nothing Or gui Is Nothing Then GoTo dn
con.CloseSession session.id
con.CloseConnection
Set app = Nothing
Set con = Nothing
Set session = Nothing
dn:
C1ose = True
eh:
End Function