How to automate how to process mass transaction without ABAP code (SAP ABAP coding 하지 않고 엑셀 매크로 VBA로 SAP 생산오더 만들기)
SAP 프로젝트를 하다보면 개발자의 도움없이 특정 Tranaction을 대량으로 자동으로 실행시킬 경우가 있다. 물론 ABAP coding 을 배워서 하든지 ABAP개발자가 개발 하면 가능하다. 대량 Data를 엑셀로 정리하여 개발된 프로그램으로 실행하면 된다. 만약 상황이 여의치 않고, 본인이 엑셀 매크(VBA)를 조금 해본 경험이 있다면 아래 VBA source code 를 복사하여 엑셀로 개발하면 가능하다. 아래 SAP 생산오더 생성 및 변경을 위한 엑셀 매크로 VBA를 참조하시기 바랍니다.
물론 생산오더 포함하여 SAP recording 이 가능한 모든 Transaction 에 대해 적용 가능하다. 다른 Transaction 을 실행하고 싶다면 SAP script recording를 실행하면 자동으로 VB source가 recording 되므로 크게 걱정할 필요는 없습니다. Recording 된 VBA source 파일(*.vbs)을 열어 "session.findById" 시작하는 문장을 엑셀로 복사하여 수정하면 된다.
그러면 Scirpt Recording 시작해보자.
1. 아래 화면을 참조하여 Script Recording 기능을 활성화 해주어야 한다.
1.1. 먼저 t-code RZ11을 실행합니다.
3. 아래 화면을 참조하여 Script Recording을 한다.
3.1 Script Recording을 저장할 file path 및 file 이름을 지정한다.
3.2 원하는 t-code를 실행한다.>> For English Readers
* Please find VBA source code below to create / change process order COR1 & COR2
Private Sub SAP_Process_Order(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim SapGui As Object
Dim Applic As Object
Dim connection As Object
Dim session As Object
Dim WSHShell As Object
Dim ObjR3 As Object
Dim Grid As Object
Dim sap_message As String
Dim start_date As String
Dim end_date As String
' Dim objBAPIControl As Object 'Function Control (Collective object)
' Dim sapConnection As Object 'Connection object
' Set objBAPIControl = CreateObject("SAP.Functions")
' Set sapConnection = objBAPIControl.connection
' Set ObjR3 = CreateObject("SAP.Functions")
If Me.txtMaterialCode.Value <> "" And Me.txtStartDate.Value <> "" And Me.txtEndDate.Value <> "" And Me.txtQty.Value <> "" Then
If Me.txtOrder.Value <> "" Then
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
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
If shHome.Cells(3, 6).Value = "xxP" Then 'Choose SAP server Quality or Production server ?
Set connection = Applic.OpenConnection("0.3 ECC ? PROD (xxP)", True)
Else
Set connection = Applic.OpenConnection("0.2 ECC ? QUAL (xxQ)", True)
End If
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = shHome.Cells(2, 6).Value
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = shHome.Cells(4, 6).Value
session.findById("wnd[0]").sendVKey 0
'Start of COR2, Change Process Order
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nCOR2"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtCAUFVD-AUFNR").Text = Me.txtOrder.Value
session.findById("wnd[0]").sendVKey 0
'end of COR2
Else
'Start of COR1, Create Process Order
If CDate(Me.txtStartDate.Value) >= Format(Now(), "yyyy-mm-dd") And CDate(Me.txtEndDate.Value) >= Format(Now(), "yyyy-mm-dd") Then
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
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
If shHome.Cells(3, 6).Value = "xxP" Then
On Error Resume Next
Set connection = Applic.OpenConnection("0.3 ECC ? PROD (xxP)", True)
Else
On Error Resume Next
Set connection = Applic.OpenConnection("0.2 ECC ? QUAL (xxQ)", True)
End If
Set session = connection.Children(0)
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = shHome.Cells(2, 6).Value
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = shHome.Cells(4, 6).Value
session.findById("wnd[0]").sendVKey 0
'change date format yyyy-mm-dd to dd.mm.yyyy
start_date = Right(Me.txtStartDate.Value, 2) & "." & Mid(Me.txtStartDate.Value, 6, 2) & "." & Left(Me.txtStartDate.Value, 4)
end_date = Right(Me.txtEndDate.Value, 2) & "." & Mid(Me.txtEndDate.Value, 6, 2) & "." & Left(Me.txtEndDate.Value, 4)
session.findById("wnd[0]/tbar[0]/okcd").Text = "/NCOR1" 'Type T-Code to create Process Order in command window
session.findById("wnd[0]").sendVKey 0 'Press Enter T-Code to create Process Order
On Error GoTo ErrorHandler
session.findById("wnd[0]/usr/ctxtCAUFVD-MATNR").Text = Me.txtMaterialCode.Value 'Enter Material Code
session.findById("wnd[0]/usr/ctxtCAUFVD-WERKS").Text = "xxxx" 'Enter Plant Code
session.findById("wnd[0]/usr/ctxtCAUFVD-WERKS").SetFocus
session.findById("wnd[0]/usr/ctxtCAUFVD-WERKS").caretPosition = 4
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOZE/ssubSUBSCR_5115:SAPLCOKO:5120/txtCAUFVD-GAMNG").Text = Me.txtQty.Value 'Enter QTY
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOZE/ssubSUBSCR_5115:SAPLCOKO:5120/ctxtCAUFVD-GLTRP").Text = end_date 'Enter end date of Process Order
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOZE/ssubSUBSCR_5115:SAPLCOKO:5120/ctxtCAUFVD-GSTRP").Text = start_date 'Enter start date of Process Order
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOZE/ssubSUBSCR_5115:SAPLCOKO:5120/ctxtCAUFVD-GSTRP").SetFocus
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOZE/ssubSUBSCR_5115:SAPLCOKO:5120/ctxtCAUFVD-GSTRP").caretPosition = 10
session.findById("wnd[0]/tbar[1]/btn[30]").press 'Press Release button on application toolbar
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOWE").Select
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOWE/ssubSUBSCR_5115:SAPLCOKO:5190/ctxtAFPOD-CHARG").SetFocus
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOWE/ssubSUBSCR_5115:SAPLCOKO:5190/ctxtAFPOD-CHARG").caretPosition = 4
If Me.txtBatch.Value <> "" Then
session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOWE/ssubSUBSCR_5115:SAPLCOKO:5190/ctxtAFPOD-CHARG").Text = Me.txtBatch.Value 'Enter batch No.
session.findById("wnd[0]/tbar[0]/btn[11]").press
session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
Else
Me.txtBatch.Value = session.findById("wnd[0]/usr/tabsTABSTRIP_5115/tabpKOWE/ssubSUBSCR_5115:SAPLCOKO:5190/ctxtAFPOD-CHARG").Text 'Enter Batch No
End If
session.findById("wnd[0]/tbar[0]/btn[11]").press 'Press Save button
sap_message = session.findById("wnd[0]/sbar").Text 'Fectch system message to extract Process order No
Me.txtOrder.Value = Mid(sap_message, 9, 7) 'Extract Process order No
Set session = Nothing
connection.CloseSession ("ses[0]") 'Logoff
Set connection = Nothing
Else
MsgBox "Check start and end date"
End If
End If
Else
MsgBox "Check SAP Material code, Date, QTY"
End If
Exit Sub
ErrorHandler:
Set session = Nothing
On Error Resume Next
'connection.CloseSession ("ses[0]")
Set connection = Nothing
MsgBox "Log off all SAP screen"
Exit Sub
End Sub
Comments
Post a Comment