2023 Sep 19 2:56 AM
Hi, all
I have created a macro in VBA code to run SAP, but I'm encountering a runtime error 438 in the final part. Could it be that I made a mistake in my code?
I don't have any knowledge about coding; my background is in mechanical engineering. I put together the code with the help of ChatGPT and internet research.
So, I will be very appriciate if you explain detail.
Thanks.
Private Sub OKButton_Click()
Dim inputData As String
Dim SapGui, Applic, connection, session, WSHShell
Dim sourceFileName As String
Dim sourceWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim sourceWorksheet As Worksheet
Dim targetWorksheet As Worksheet
inputData = PJTName.Text
inputID = SAPID.Text
inputPW = SAPPW.Text
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
Set connection = Applic.OpenConnection("SAP Production R3", True)
Set session = connection.Children(0)
Set SapGuiAPP = CreateObject("SAPGUI.ScriptingCtrl.1")
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "050"
session.findById("wnd[0]/usr/txtRSYST-BNAME").Text = inputID
session.findById("wnd[0]/usr/pwdRSYST-BCODE").Text = inputPW
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[0]/okcd").Text = "cs12"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRC29L-MATNR").Text = inputData
session.findById("wnd[0]/usr/ctxtRC29L-WERKS").Text = "7036"
session.findById("wnd[0]/usr/ctxtRC29L-CAPID").Text = "pp01"
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/tbar[1]/btn[43]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").Select
session.findById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[0,0]").SetFocus
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
On Error Resume Next
' 현재 열려있는 엑셀 파일을 지정
Set sourceWorkbook = ActiveWorkbook
On Error GoTo 0
If Not sourceWorkbook Is Nothing Then
Set sourceWorksheet = sourceWorkbook.Sheets("Sheet1")
Set targetWorkbook = ThisWorkbook
sourceWorksheet.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
Set targetWorksheet = targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
targetWorksheet.Name = inputData ' 원하는 이름으로 변경
session.findById("wnd[1]/tbar[0]/btn[0]").press
"I'm encountering a runtime error 438 at this part"
SapGuiAPP.Application.Quit
MsgBox "Complete"
Set sourceWorksheet = Nothing
Set targetWorksheet = Nothing
Set sourceWorkbook = Nothing
Set targetWorkbook = Nothing
Set SapGuiAPP = Nothing
Set session = Nothing
Set Applic = Nothing
Set SapGui = Nothing
Set connection = Nothing
Else
MsgBox "Not completed"
End If
Unload Me
End Sub