Connection to SAP HANA via Excel VBA

How to connect SAP HANA via Excel VBA? Following code allows to connect SAP and make operation inside of SAP. This in only a part of code only to connect SAP. Others elements such as recorded SAP code and Excel data processing code should be add to project.

To use Excel connection to SAP code reference SAP GUI Scripting API has to be add to project. Additionally user has to be logged to SAP to run code.

One test procedure is added to connection code in order to show where recorded SAP code or Excel processing data code should be pasted. Additionally error handling is turned on. Please have look on declared global variables, these variables might be used in all project without declaring it in every module.

Excel connection to SAP HANA example:

Option Explicit

' --- MODULE INFO  -----------------------------------------------------------------------------------------------
' This is only wireframe of connection between EXCEL VBA and SAP HANA. In order to connect SAP user has to be 
' logged to SAP.
' Application checks if main SAP windows is opened this SESSION_MANAGER – if not application returns error. If main
' window is opened code is proceeding. 
' Error handling has been added to this SAP connection code. In order to run code, reference SAP GUI Scripting API need
' to be add to project
'
' ADDING REREFENCE TO PROJECT
' SAP GUI Scripting API (file sapfewse.ocx in folder SAP C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewse.ocx)
'
' TURNING OFF SAP ALERTS – SAP MSGBOX during external connection
' SAP GUI Scripting API (plik sapfewse.ocx w folderze SAP C:\Program Files (x86)\SAP\FrontEnd\SAPgui\sapfewse.ocx)

' ------------------------------------------------------------------------------------------------------------------
    
' Source:
' https://blogs.sap.com/2013/03/29/use-the-vba-debugger-to-analyze-sap-gui-scripting-objects-at-runtime/
' https://archive.sap.com/discussions/thread/3551900
' https://archive.sap.com/discussions/thread/3517188
' https://windowssecrets.com/forums/showthread.php/153057-I-want-Excel-always-on-top
' and others

Public SGA As Object
Public App As SAPFEWSELib.GuiApplication
Public Connection As SAPFEWSELib.GuiConnection
Public Session As SAPFEWSELib.GuiSession
Public SessionTcode, ModulName, procName As String
Public ErrorHapenned, ErrStatus, ErrOpis
Public ErlLine As Long

' objxl declaration
#If VBA7 Then
    Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
    Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If

Public Sub SAPconnectionTest()
' User has to be logged in SAP
' SAP SESSION_MANAGER has to be opened
' To see code confirmation/information IMMEDIATE WINDOW should be run

Dim LicznikOkienekSAP As Integer
Dim StartTime, EndTime, FinalTime, SapWindowJump
Dim objxl As Object 'przywracanie Excela na górę
Dim SapWindowsCount As Long

On Error GoTo ErrorHapenned

' Values assignment to variables
Set objxl = GetObject(, "Excel.Application")
StartTime = Format(Time, "Long Time")
Application.ScreenUpdating = False
ModulName = "modSAPconnection"
procName = "SAPconnectionTest"
ErrStatus = False

' ---------------------------------------------------------------------------
' SET UP NEW CONNECTION TO SAP

Set SGA = GetObject("SAPGUI")
If Not IsObject(SGA) Then
GoTo ErrorHapenned
End If

Set App = SGA.GetScriptingEngine()
If Not IsObject(App) Then
GoTo ErrorHapenned
End If

Set Connection = App.Connections(0)
If Not IsObject(Connection) Then
GoTo ErrorHapenned
End If

' ----------------------------------------------------------
' SEARCHING SAP SESSION MANAGER AND DOWNLOAD NEEDED DATA

SapWindowsCount = Connection.Children.Count 'returns number of opened SAP windows
For LicznikOkienekSAP = 0 To SapWindowsCount - 1
' Sprawdzanie nazw transakcji SAP
Set Session = Connection.Children(CInt(LicznikOkienekSAP))
SessionTcode = Session.Info.Transaction
If SessionTcode = "SESSION_MANAGER" Then
        
    ' SAP window on top
    Session.ActiveWindow.JumpForward

' --------------------------------------------------------
' STARTING VBA CODE WORKING IN SAP (code has should be recorded in SAP)

     ' Confirmation of correct SAP connection / should be deleted
    Debug.Print StartTime & " Excel -> SAP connection. Status: OK."
    
    ' Example how to use ErrStatus variable
    If ErrStatus = True Then
        GoTo ErrorHapenned
    Else
        ErrorShow 'call other procedure (error can be raised there to see how error handle works)
    End If
    
    If ErrStatus = True Then
        GoTo ErrorHapenned
    Else
        Debug.Print StartTime & " SAP data download. Status: OK"
    End If

' END VBA CODE WORKING IN SAP
' ----------------------------------------------------------
 
    ' Cleaning after SAP connection
    Set Session = Nothing
    Set Connection = Nothing
    Set App = Nothing
    Set SGA = Nothing
    
    ' Code jump if there is no finding SESSION MANAGER error,
    ' this is main SAP window
    GoTo SapWindowJump

    End If
Next LicznikOkienekSAP

'    MsgBox "Before start app SAP 'SESSION MANAGERA' has to be opened," & _
'            Chr(10) & "this is main SAP window.", vbCritical, "ATTENTION..."
    Debug.Print StartTime & " SAP main windows is not opened. Status: ERROR."
    Exit Sub
   
SapWindowJump:

' ----------------------------------------------------------
' STARTING CODE WORKING IN EXCEL (downloaded data processing)

ModulName = "modSAPconnection"
procName = "SAPconnectionTest"
objxl.Visible = True
SetForegroundWindow objxl.hwnd 'Excel on top?

' Data processing
If ErrStatus = True Then
    GoTo ErrorHapenned
Else
    Debug.Print StartTime & " SAP data processing in Excel . Status: OK."
End If

' END OF CODE WORKING IN EXCEL
' ----------------------------------------------------------


' ----------------------------------------------------------
' CLEANING

Application.ScreenUpdating = True
EndTime = Format(Time, "Long Time")
FinalTime = Format(CDate(EndTime) - CDate(StartTime), "Long Time")
 
On Error GoTo 0
Exit Sub

' ----------------------------------------------------------
' ERROR HANDLING

ErrorHapenned:
MsgBox "CODE ERROR!" & Chr(10) & Chr(10) & _
                "Modul: " & ModulName & Chr(10) & _
                "Procedure: " & procName & Chr(10) & _
                "Line: " & Erl & Chr(10) & _
                "Desc.: " & ErrOpis, vbCritical, "APP ERROR..."
Err.Clear

' ----------------------------------------------------------
' CLEANING

Set Session = Nothing
Set Connection = Nothing
Set App = Nothing
Set SGA = Nothing

End Sub


Public Sub ErrorShow()
' Raising test error. Normally here should be pasted SAP operation code, or processing code in Excel
' In order to rise error to long variable "i" assign some string value
' If you don't raise error leave it as it is

Dim i As Long

On Error GoTo ErrorHapenned
procName = "ErrorShow"
i = 1
'i = "Assing string to long = error"
Exit Sub

' ----------------------------------------------------------
' ERROR HANDLING

ErrorHapenned:
ErrOpis = Err.Description
ErrStatus = True
ErlLine = Erl 'variable "erl" get value when code lines will be added to project
End Sub

183 thoughts on “Connection to SAP HANA via Excel VBA

  1. Pingback: Get SAP format date | My Excel Database

  2. Pingback: Tyson vs Jones jr live stream online

  3. Pingback: essay help toronto

  4. Pingback: stimulant vyvanse

  5. Pingback: best dumps site

  6. Pingback: help with college essay

  7. Pingback: help writing dissertation

  8. Pingback: custom essay paper writing

  9. Pingback: essay about community service

  10. Pingback: dumps shop 2020

  11. Pingback: write my papers

  12. Pingback: writing thesis paper

  13. Pingback: write my research paper for me for free

  14. Pingback: thesis express

  15. Pingback: house cleaning services south bend in

  16. Pingback: buy driving license online

  17. Pingback: Sporting Goods Store

  18. Pingback: นิยายจีน pdf

  19. Pingback: more subscribers on youtube

  20. Pingback: build immunity with cbd

  21. Pingback: nằm mơ thấy cá lóc

  22. Pingback: cold war cheap

  23. Pingback: mơ bị sét đánh

  24. Pingback: good dumps shop

  25. Pingback: mơ thấy đi tàu hỏa

  26. Pingback: mơ thấy lợn đẻ

  27. Pingback: nằm mơ thấy em trai

  28. Pingback: nằm mơ thấy mua nhà

  29. Pingback: mơ thấy tóc bạc

  30. Pingback: cialis generic name

  31. Pingback: free cc dumps

  32. Pingback: tadalafil for sale

  33. Pingback: viagra vs cialis vs levitra

  34. Pingback: cialis and alcohol

  35. Pingback: cvv dumps sites

  36. Pingback: 토토사이트 추천

  37. Pingback: best place to buy viagra online

  38. Pingback: where to buy viagra

  39. Pingback: can you buy viagra over the counter

  40. Pingback: viagra samples

  41. Pingback: More Info

  42. Pingback: white leaf

  43. Pingback: nằm mơ thấy rùa đen

  44. Pingback: drugs without prescription

  45. Pingback: pharmacies online

  46. Pingback: online pharmacy without scripts

  47. Pingback: canada drugs online

  48. Pingback: Himplasia

  49. Pingback: Buy Dumps with PIN Online

  50. Pingback: viagra

  51. Pingback: gravatar wordpress

  52. Pingback: cialis or viagra

  53. Pingback: taking cialis soft tabs

  54. Pingback: generis cialis online overnight delivery

  55. Pingback: buy cialis philippines

  56. Pingback: find cheap cialis online

  57. Pingback: buy cheap cialis online with mastercard

  58. Pingback: viagracheap

  59. Pingback: buy cialis tadalafil tablets

  60. Pingback: buy cialis online in canada

  61. Pingback: cialis 20mg for sale

  62. Pingback: cost of cialis without insurance

  63. Pingback: luvcntpf

  64. Pingback: what strength does viagra come in

  65. Pingback: hoe gevaarlijk is viagra

  66. Pingback: where can i buy zithromax over the counter in florida

  67. Pingback: buy viagra cialis online

  68. Pingback: taking cialis soft tabs

  69. Pingback: buy viagra

  70. Pingback: buy cialis tadalafil tablets

  71. Pingback: viagra pastillas

  72. Pingback: cialis for daily use

  73. Pingback: custom dissertation writing service

  74. Pingback: website where my research paper can be written

  75. Pingback: pay someone to write my college essay

  76. Pingback: help me write a 5 paragraph essay

  77. Pingback: essay on issues in business ethics

  78. Pingback: taking cialis soft tabs

  79. Pingback: taking cialis soft tabs

  80. Pingback: amoxicillin 500 capsule

  81. Pingback: buy lasixonline

  82. Pingback: generic for azithromycin

  83. Pingback: ivermectin pills canada

  84. Pingback: albuterol prescription drug

  85. Pingback: what's doxycycline

  86. Pingback: prednisolone mg

  87. Pingback: cycle after clomid

  88. Pingback: dapoxetine online uae

  89. Pingback: diflucan two doses

  90. Pingback: synthroid and calcium

  91. Pingback: prescription for propecia

  92. Pingback: neurontin lawsuit 2015

  93. Pingback: metformin strengths

  94. Pingback: paxil headache relief

  95. Pingback: plaquenil supply

  96. Pingback: metformin 500 mg otc

  97. Pingback: generic propecia without prescription

  98. Pingback: cheap finasteride

  99. Pingback: buy cialis rush

  100. Pingback: medications for ed

  101. Pingback: new ed pills

  102. Pingback: legitimate canadian online pharmacies

  103. Pingback: buy cialis with paypal

  104. Pingback: Zakhar Berkut hd

  105. Pingback: canadian pharmacy service

  106. Pingback: 4569987

  107. Pingback: buy cialis canadian

  108. Pingback: buy cialis online cheap

  109. Pingback: buy tadalafil0 with pay pal

  110. Pingback: news news news

  111. Pingback: faxless payday loans southfield

  112. Pingback: where can i buy without a prescription

  113. Pingback: psy

  114. Pingback: psy2022

  115. Pingback: projectio-freid

  116. Pingback: viagra with retinitis pigmentosa

  117. Pingback: tinder trap

  118. Pingback: cash advance online redwood city

  119. Pingback: kinoteatrzarya.ru

  120. Pingback: topvideos

  121. Pingback: video

  122. Pingback: order original online

  123. Pingback: is viagra a controlled substance

  124. Pingback: 20mg for sale

  125. Pingback: order original online

  126. Pingback: zestoretic 20 25 mg

  127. Pingback: afisha-kinoteatrov.ru

  128. Pingback: Ukrainskie-serialy

  129. Pingback: site

  130. Pingback: free no photo dating

  131. Pingback: lisinopril 20 mg prices

  132. Pingback: legal buy ativan online

  133. Pingback: cialis for free

  134. Pingback: canadian pharmacies cialis

  135. Pingback: cialis viagra levitra canada

  136. Pingback: top

  137. Pingback: canadian pharmacy no prescription

  138. Pingback: buy cialis online cheap

  139. Pingback: canadian pharmacy viagra reviews

  140. Pingback: buy viagra cialis online

  141. Pingback: online pharmacy tech training

  142. Pingback: cialis generic 20 mg 30 pills

  143. Pingback: buy cialis cheap canada

  144. Pingback: buy cialis united kingdom

  145. Pingback: buy cialis cheap canada

  146. Pingback: cialis online without prescription

  147. Pingback: muse for ed

  148. Pingback: buy valtrex without a prescription

  149. Pingback: cures for ed

  150. Pingback: male dysfunction

  151. Pingback: buy generic 100mg viagra online

  152. Pingback: soderzhanki-3-sezon-2021.online

  153. Pingback: chelovek-iz-90-h

  154. Pingback: podolsk-region.ru

  155. Pingback: herbal ed

  156. Pingback: bender na4alo 2021

  157. Pingback: blogery_i_dorogi

  158. Pingback: blogery_i_dorogi 2 blogery_i_dorogi

  159. Pingback: ed medication online

  160. Pingback: cost of ivermectin

  161. Pingback: 60mg generic priligy pill

  162. Pingback: plaquenil 200 mg prices

  163. Pingback: plaquenil price singapore

  164. Pingback: stromectol 3 mg

  165. Pingback: albuterol oral tablet

  166. Pingback: ivermectin 3

  167. Pingback: lancet hydroxychloroquine

  168. Pingback: ed treatments

  169. Pingback: viagra vs cialis bodybuilding

  170. Pingback: chernaya vodova

  171. Pingback: 66181

  172. Pingback: Porno

  173. Pingback: vechernyy urgant

  174. Pingback: ukraine

  175. Pingback: viagra cost per pill

  176. Pingback: best place to buy viagra online

  177. Pingback: A3ixW7AS

  178. Pingback: cialis 20 mg cost

  179. Pingback: Google

  180. Pingback: buy cialis without perscription

  181. Pingback: hydroxychloroquinee

Leave a Reply

Your email address will not be published. Required fields are marked *