Get ID & Text of each control in SAP window
Sub Get_SAP_TableIDs()
Dim SapGuiAuto As Object
Dim SAPApp As Object
Dim SAPCon As Object
Dim session As Object
Dim ctrl As Object
' define objects
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApp = SapGuiAuto.GetScriptingEngine
Set SAPCon = SAPApp.Children(0)
Set session = SAPCon.Children(0)
' Search all the controls on SAP screen
For Each ctrl In session.FindById("wnd[0]/usr").Children
Debug.Print ctrl.ID
Debug.Print ctrl.Text
' If InStr(1, ctrl.ID, "tbl") > 0 Or InStr(1, ctrl.ID, "shell") > 0 Then
' Debug.Print "Possible Table/Grid ID: " & ctrl.ID & " | Type: " & ctrl.Type
' End If
Next ctrl
MsgBox "Check Immediate Window (Ctrl+G) for result"
End Sub
Code explanation:
यह VBA Macro – Get_SAP_TableIDs Excel VBA के माध्यम से SAP GUI Scripting का उपयोग करके SAP Screen पर मौजूद Table, Grid और Controls के IDs निकालने के लिए बनाया गया है। यह Macro खासतौर पर SAP Automation, SAP Report Extraction और Excel–SAP Integration में बहुत उपयोगी है।
Macro की शुरुआत में SAP से जुड़ने के लिए जरूरी Objects Declare किए गए हैं, जैसे SapGuiAuto, SAPApp, SAPCon और session।
GetObject("SAPGUI") के जरिए पहले से खुले हुए SAP GUI Session को Access किया जाता है। इसके बाद Scripting Engine, Connection और Session को Define किया जाता है।
session.FindById("wnd[0]/usr").Children Loop के माध्यम से SAP की Current Screen पर मौजूद सभी Controls को एक-एक करके Read किया जाता है।
हर Control का ID और Text Debug.Print की मदद से Immediate Window (Ctrl + G) में Print हो जाता है।
Comment किए गए Code में यह Logic दिया गया है कि अगर Control ID में "tbl" या "shell" हो, तो वह SAP Table या Grid Control हो सकता है। इसे जरूरत के अनुसार Activate किया जा सकता है।
अंत में MsgBox User को बताता है कि Result देखने के लिए Immediate Window Check करें।
यह Macro SAP GUI Scripting VBA, Find SAP Table ID, SAP Automation with Excel, Advanced VBA for SAP जैसे SEO Keywords के लिए बहुत उपयोगी है और SAP Data Automation को आसान बनाता है।
Other VBA codes for SAP
'new window
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n"
Code explanation:
यह SAP GUI Scripting Command Excel VBA या SAP Automation में SAP Transaction को Exit या Reset करने के लिए उपयोग किया जाता है।
session.findById("wnd[0]/tbar[0]/okcd").Text = "/n" लाइन SAP के OK Code (Command Field) में /n लिखती है।
/n Command का मतलब होता है Current Transaction को बंद करके SAP Main Screen पर जाना। इससे बिना SAP को बंद किए, नया Transaction शुरू किया जा सकता है।
यह Command SAP Automation Scripts में बहुत उपयोगी है, खासकर तब जब एक Transaction पूरा होने के बाद अगला Transaction Run करना हो।
यह Code SAP GUI Scripting VBA, SAP OK Code /n, Exit SAP Transaction, SAP Automation with Excel जैसे SEO Keywords के लिए उपयोगी है और Automation Flow को Smooth बनाता है।
'enter T-code
session.findById("wnd[0]/tbar[0]/okcd").Text = "su3"
Code explanation:
यह SAP GUI Scripting Command SAP में SU3 Transaction को Open करने के लिए उपयोग किया जाता है।
session.findById("wnd[0]/tbar[0]/okcd").Text = "su3" line SAP के OK Code (Command Field) में SU3 type करती है।
SU3 Transaction का उपयोग SAP User अपनी Personal Settings जैसे User Name, Email ID, Language, Default Printer और Time Zone बदलने के लिए करता है।
यह Command आमतौर पर Excel VBA + SAP GUI Scripting Automation में इस्तेमाल होती है, ताकि Script के जरिए सीधे SU3 Screen Open हो जाए।
यह Code SAP GUI Scripting VBA, SU3 Transaction Automation, SAP User Profile Settings, SAP Automation with Excel जैसे SEO Keywords के लिए उपयोगी है और SAP Automation को तेज और आसान बनाता है।
'press enter key
session.findById("wnd[0]").sendVKey 0
Code explanation:
यह SAP GUI Scripting Command SAP में Enter Key (Execute Action) को Automatically Press करने के लिए उपयोग किया जाता है।
session.findById("wnd[0]").sendVKey 0 का मतलब होता है SAP Screen पर Enter Button दबाना।
जब आप OK Code में कोई Transaction Code (जैसे SU3, VA03, ME23N) लिखते हैं, तब इस Command से उसे Execute किया जाता है। यह Manual Enter Key Press करने की जरूरत खत्म कर देता है।
यह Line SAP Automation with Excel VBA में बहुत उपयोगी है, खासकर तब जब Multiple Transactions को लगातार Run करना हो।
यह Code SAP GUI Scripting VBA, sendVKey 0, SAP Enter Key Automation, SAP Transaction Execute जैसे SEO Keywords के लिए उपयोगी है और Automation Flow को Smooth बनाता है।
'Click on Generate/Replace file button
If Len(Dir(OutputPathSAP & Application.PathSeparator & "Closing balance" & ".xlsx")) = 0 Then
session.findById("wnd[1]/tbar[0]/btn[0]").Press
Else
session.findById("wnd[1]/tbar[0]/btn[11]").Press
End If
Code explanation:
यह VBA + SAP GUI Scripting Logic SAP से Excel File Export करते समय Duplicate File Handling के लिए उपयोग किया जाता है।
Dir Function यह Check करता है कि दिए गए Output Path में "Closing balance.xlsx" file पहले से मौजूद है या नहीं।
अगर Len(Dir(...)) = 0 है, तो इसका मतलब है कि file मौजूद नहीं है। ऐसे में
btn[0] Press किया जाता है, जो आमतौर पर Save / Yes Button होता है।
अगर file पहले से मौजूद है, तो
btn[11] Press किया जाता है, जो Overwrite / Replace Option को Select करता है।
यह Logic SAP Automation with Excel VBA में बहुत जरूरी है, ताकि Export Process बिना Error के चले और Manual Intervention की जरूरत न पड़े।
यह Code SAP GUI Scripting VBA, SAP File Export Automation, Handle Existing File SAP, Excel SAP Automation जैसे SEO Keywords के लिए उपयोगी है।
'Code to By-pass Information windows
Do While session.ActiveWindow.Text Like "*Information"
session.findById("wnd[1]/tbar[0]/btn[0]").press
Loop
Code explanation:
यह SAP GUI Scripting VBA Loop SAP में आने वाले Information Pop-up Messages को Automatically Close करने के लिए उपयोग किया जाता है।
Do While session.ActiveWindow.Text Like "*Information" यह Check करता है कि Active Window का Title Information है या नहीं।
जब तक Information Pop-up खुला रहता है,
session.findById("wnd[1]/tbar[0]/btn[0]").Press Command OK / Continue Button को Automatically Press करता रहता है।
इससे SAP Automation Script Manual Click के बिना आगे बढ़ता है और Process रुकता नहीं है।
यह Logic खासतौर पर तब काम आती है जब SAP Export, Save या Transaction Run करते समय बार-बार Information Messages आते हैं।
यह Code SAP GUI Scripting VBA, Handle SAP Information Popup, SAP Automation Loop, Excel SAP Automation जैसे SEO Keywords के लिए बहुत उपयोगी है।
'close pop up if title is Information
If UCase(Session.ActiveWindow.text) = UCase("Information") Then
Session.findById("wnd[1]/tbar[0]/btn[0]").press
'Session.findById("wnd[1]/tbar[0]/btn[12]").press
End If
Code explanation:
यह SAP GUI Scripting VBA Condition SAP में आने वाले Information Popup को Automatically Handle करने के लिए उपयोग की जाती है।
UCase(Session.ActiveWindow.Text) का उपयोग Text को Upper Case में Convert करके Case-Sensitive Error से बचने के लिए किया गया है।
अगर Active Window का Title “Information” होता है, तो
btn[0] Press किया जाता है, जो आमतौर पर OK / Continue Button होता है। इससे Popup अपने-आप Close हो जाता है।
Comment की गई Line btn[12] वैकल्पिक Button (जैसे Cancel या Back) के लिए रखी गई है।
यह Logic SAP Automation with Excel VBA में बहुत जरूरी है, ताकि Script बीच में न रुके।
यह Code SAP GUI Scripting VBA, Handle SAP Information Popup, SAP Message Automation, Excel SAP Integration जैसे SEO Keywords के लिए उपयोगी है।
'save SAP screenshot to folder
session.findById("wnd[0]").HardCopy ThisWorkbook.Path & "\Dump\" & POLINE_NUMBER & "\Images\Sapshot_" & M + 1, 1
Code explanation:
यह SAP GUI Scripting Command SAP Screen का Automatically Screenshot (Hard Copy) लेने के लिए उपयोग किया जाता है।
session.findById("wnd[0]").HardCopy SAP की Current Window की Image Save करता है।
यह Screenshot Excel Workbook के Path में दिए गए Folder Structure में Store होता है, जहाँ
POLINE_NUMBER और M + 1 के आधार पर Dynamic File Name बनता है, जैसे Sapshot_1।
1 Parameter का मतलब होता है कि Screenshot Front-End Graphic Mode में लिया जाए।
यह Command SAP Automation, Audit Proof, Error Evidence और Documentation के लिए बहुत उपयोगी है।
यह Code SAP GUI Scripting VBA, SAP Screenshot Automation, HardCopy Command SAP, Excel SAP Automation जैसे SEO Keywords के लिए उपयोगी है और Process को Professional बनाता है।
Get SAP date format
Sub SAP_Connect()
Dim SapGuiAuto As Object
Dim connection As Object
Dim FSO As New FileSystemObject
With ThisWorkbook
.Activate
With .Sheets("Mapping")
.Activate
LastRow = .Cells(.Rows.Count, 8).End(xlUp).Row
If LastRow = 1 Then
Else
.Range("H2:H" & LastRow).Copy
If SapGuiAuto Is Nothing Then
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
Set app = SapGuiAuto.GetScriptingEngine
On Error GoTo 0
End If
If SapGuiAuto Is Nothing Then
MsgBox "SAP is not Running", vbCritical, "Please Check"
End
End If
If connection Is Nothing Then
On Error Resume Next
Set connection = app.Children(0): Set session = connection.Children(0)
On Error GoTo 0
End If
If connection Is Nothing Then
Else: Set connection = app.Children(0): Set session = connection.Children(0)
End If
If connection Is Nothing Then
MsgBox "SAP Session is not Running", vbCritical, "Please Check"
End
Else: Set connection = app.Children(0): Set session = connection.Children(0)
End If
'storing the date SAP date format
session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "su3"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA").Select
TempDateFormat = session.findById("wnd[0]/usr/tabsTABSTRIP1/tabpDEFA/ssubMAINAREA:SAPLSUID_MAINTENANCE:1105/cmbSUID_ST_NODE_DEFAULTS-DATFM").text
session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
session.findById("wnd[0]").sendVKey 0
SapDateFormat = Trim(TempDateFormat)
If UBound(Split(SapDateFormat, " ")) - LBound(Split(SapDateFormat, " ")) + 1 >= 2 Then
SapDateFormat = Split(SapDateFormat, " ")(1)
End If
sdate = Format(AsOnDate, SapDateFormat) 'date for tcodes
tdate = Format(ThisWorkbook.Sheets(Sheet2.Name).Range("C16"), SapDateFormat) 'date for job scheduling
STime = Format(ThisWorkbook.Sheets(Sheet2.Name).Range("C17"), "hh:mm:ss")
session.findById("wnd[0]/tbar[0]/okcd").text = "/n"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").text = "FBL3N"
End Sub
Code explanation:
यह VBA Macro – SAP_Connect Excel को SAP GUI से Automatically Connect करने और आगे की SAP Automation Process की तैयारी के लिए बनाया गया है। यह Macro खासतौर पर तब उपयोगी होता है जब Excel से SAP Transactions (जैसे FBL3N) Run करनी हों।
Macro की शुरुआत में Workbook और Mapping Sheet को Activate किया जाता है और Column H में मौजूद Data की Last Row निकाली जाती है। इसके बाद Code Check करता है कि SAP GUI पहले से Running है या नहीं।
GetObject("SAPGUI") के जरिए SAP Scripting Engine, Connection और Session को Access किया जाता है। अगर SAP या Session नहीं मिलता, तो Error Message दिखाकर Process रोक दी जाती है।
इसके बाद SAP Command Field में /n डालकर SAP को Reset Screen पर लाया जाता है और Screen को Maximize किया जाता है।
फिर SU3 Transaction Open की जाती है ताकि SAP User की Date Format Setting पढ़ी जा सके।
User Default Tab से SAP का Date Format निकाला जाता है और उसे Excel Dates के साथ Match किया जाता है। इससे यह सुनिश्चित होता है कि SAP में Date गलत Format की वजह से Error न दे।
अंत में SAP को फिर Reset करके FBL3N Transaction Type की जाती है, जिससे आगे Financial Data Extraction की जा सके।
यह Macro Excel VBA SAP Connection, SAP GUI Scripting Automation, SU3 Date Format Handling, SAP FBL3N Automation जैसे SEO Keywords के लिए बहुत उपयोगी है और End-to-End SAP Automation को आसान बनाता है।
Close Excel instances of SAP
Call CloseSAPExcel("F01.xlsx")
#If VBA7 Then
Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" ( _
ByVal hwnd As LongPtr, ByVal dwId As Long, riid As Any, ppvObject As Object) As Long
Private Declare PtrSafe Function FindWindowExA Lib "user32" ( _
ByVal hwndParent As LongPtr, ByVal hwndChildAfter As LongPtr, _
ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr
#Else
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" ( _
ByVal hwnd As Long, ByVal dwId As Long, riid As Any, ppvObject As Object) As Long
Private Declare Function FindWindowExA Lib "user32" ( _
ByVal hwndParent As Long, ByVal hwndChildAfter As Long, _
ByVal lpszClass As String, ByVal lpszWindow As String) As Long
#End If
Sub CloseSAPExcel(ParamArray FileNames())
'Procedure to close files which is downloaded from SAP and at the same time close the Excel application instance that will be open with them.
Dim ExcelAppSAP As Variant
Dim ExcelFile As Variant
Dim FinishedLoop As Boolean, TimeoutReached As Boolean, FileClosed As Boolean
Dim ReTry As Long
Dim i As Long, x As Long
Set ExcelAppSAP = Nothing
ReTry = 100000 'Used as Timeout 100000 = ~10 seconds
i = 1
'The following loop is executed until excel file is closed.
'Inside of this, there is a For Loop for each Excel Instance and inside of that is another loop
'for each excel inside the instance. If name matches, it is closed.
Do While Not FinishedLoop
If i > ReTry Then
TimeoutReached = True
Exit Do
End If
For Each ExcelFile In GetXlsInstances() 'Function to Get Excel Open Instances
For Each xls In ExcelFile.Workbooks
For x = LBound(FileNames()) To UBound(FileNames())
If xls.Name = FileNames(x) Then
Set ExcelAppSAP = ExcelFile 'Set Instance opened by SAP to variable
'Here add actions if needed. Reference to workbook as xls e.g.: xls.Sheets(1).Range("A1").Copy
xls.Close SaveChanges:=False
FileClosed = True
End If
Next x
Next
Next
If FileClosed Then
FinishedLoop = True
End If
i = i + 1
Loop
ThisWorkbook.Activate
If Not TimeoutReached Then
If FileClosed Then
On Error Resume Next
If ExcelAppSAP.Workbooks.Count = 0 Then
ExcelAppSAP.Quit
End If
Else
MsgBox "Excel application instance from SAP was not closed correctly. Please close it manually or try again.", , "Error"
End If
Else
MsgBox "Max timeout reached", , "Error"
End If
End Sub
Public Function GetXlsInstances() As Collection
Dim guid&(0 To 3), acc As Object, hwnd, hwnd2, hwnd3
guid(0) = &H20400
guid(1) = &H0
guid(2) = &HC0
guid(3) = &H46000000
Set GetExcelInstances = New Collection
Do
hwnd = FindWindowExA(0, hwnd, "XLMAIN", vbNullString)
If hwnd = 0 Then Exit Do
hwnd2 = FindWindowExA(hwnd, 0, "XLDESK", vbNullString)
hwnd3 = FindWindowExA(hwnd2, 0, "EXCEL7", vbNullString)
If AccessibleObjectFromWindow(hwnd3, &HFFFFFFF0, guid(0), acc) = 0 Then
GetExcelInstances.Add acc.Application
End If
Loop
End Function
Code explanation:
यह VBA Automation Code – CloseSAPExcel SAP से Downloaded Excel Files को Automatically Close करने और साथ ही उन Excel Application Instances को भी बंद करने के लिए बनाया गया है, जो SAP Export के समय Background में Open हो जाते हैं।
Call CloseSAPExcel("F01.xlsx") के जरिए उस Excel File का नाम पास किया जाता है जिसे SAP से Download किया गया है। यह Code VBA7 और पुराने VBA Versions दोनों को Support करता है, इसलिए इसमें PtrSafe API Declarations का उपयोग किया गया है।
CloseSAPExcel Sub में Loop के जरिए सभी Running Excel Instances को Search किया जाता है।
GetXlsInstances() Function Windows API का उपयोग करके हर खुले हुए Excel Application Instance को Collect करता है।
हर Excel Instance के अंदर मौजूद सभी Workbooks को Check किया जाता है। अगर Workbook का नाम Passed File Name से Match करता है, तो उसे Save किए बिना Close कर दिया जाता है। इससे SAP Automation के दौरान खुले हुए Extra Excel Files system को Slow नहीं करते।
Timeout Logic लगाया गया है, ताकि Code Infinite Loop में न फँसे।
अगर सभी Workbooks बंद हो जाते हैं, तो उस Excel Application Instance को भी Quit कर दिया जाता है।
यह Code Excel VBA SAP Automation, Close SAP Exported Excel Files, Handle Multiple Excel Instances, Advanced VBA Windows API जैसे SEO Keywords के लिए बहुत उपयोगी है और SAP–Excel Integration को Stable बनाता है।
