I need to increment some values from excel file, in SAP.
I already make script for this, but it is work only for 1 value.
I need to pick value from column A and add in value from column B.
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Public objSBar As GuiStatusbar
Public objSheet As Worksheet
Sub Changelotzise()
If objGui Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
End If
If objConn Is Nothing Then
Set objConn = objGui.Children(0)
End If
If session Is Nothing Then
Set session = objConn.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject objGui, "on"
End If
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = value from A1
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").Text = value from B1
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").SetFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").caretPosition = 2
session.findById("wnd[0]").sendVKey 11
End Sub
After this I need to pick value from A2 and B2
Can you help me?
Thank you!
I try to use this:
material = ActiveWorkbook.ActiveSheet.Range("A2").Value
lotsize = ActiveWorkbook.ActiveSheet.Range("B2").Value
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nmm02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRMMG1-MATNR").Text = material
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").Text = lotsize
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").SetFocus
session.findById("wnd[0]/usr/tabsTABSPR1/tabpSP13/ssubTABFRA1:SAPLMGMM:2000/subSUB4:SAPLMGD1:2483/ctxtMARC-DISLS").caretPosition = 2
session.findById("wnd[0]").sendVKey 11
End Sub
But I need to pick a Range not a single value
Related
Sub invoicenumber1()
Dim SAPGUIAuto As Object '
Dim SAPApplication As Object '
Dim Connection As Object '
Dim session As Object
Set SAPGUIAuto = GetObject("SAPGUI")
Set objGui = SAPGUIAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Dim selectedcountry As String
selectedcountry = ActiveWorkbook.ActiveSheet.Range("D2").Value
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nvl02"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").Text = selectedcountry
session.findById("wnd[0]/usr/ctxtLIKP-VBELN").caretPosition = 8
session.findById("wnd[0]/tbar[1]/btn[30]").press
session.findById("wnd[0]/usr/shell/shellcont[1]/shell[0]").pressButton "&FIND"
session.findById("wnd[1]/usr/txtLVC_S_SEA-STRING").Text = "invoice"
session.findById("wnd[1]/usr/txtLVC_S_SEA-STRING").caretPosition = 7
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/tbar[1]/btn[18]").press
session.findById("wnd[1]/usr/ctxtVBUK-VBELN").caretPosition = 10
End Sub
i want to copy text from sap to excel please help me with the given code
i am new to this vba
i want to copy text from this line (session.findById("wnd[1]/usr/ctxtVBUK-VBELN").caretPosition = 10)
add a watch to this (select code in VBA editor and right click -> Add watch) "(session.findById("wnd[1]/usr/ctxtVBUK-VBELN")" then you can see all attributes and you can use them as you want
I want to extract a file from SAP, however, I need the name of this file to be dynamic at the time of export, taking the time it was extracted.
For example, if it was extracted at 12:00, I need the name to be "12-00.XLSX"
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").resizeWorkingPane 142,25,false
session.findById("wnd[0]/tbar[0]/okcd").text = "zv15"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtP_VKORG").text = "f86c"
session.findById("wnd[0]/usr/ctxtP_VKBUR").text = "br01"
session.findById("wnd[0]/usr/ctxtS_MONTH-LOW").text = "1"
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").text = "12"
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").setFocus
session.findById("wnd[0]/usr/ctxtS_MONTH-HIGH").caretPosition = 2
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/mbar/menu[4]/menu[11]").select
session.findById("wnd[1]/usr/ctxtLAST_LOGON_TIME").setFocus
session.findById("wnd[1]/usr/ctxtLAST_LOGON_TIME").caretPosition = 8
session.findById("wnd[1]").close
session.findById("wnd[0]/tbar[1]/btn[43]").press
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "11-50-30.XLSX"
session.findById("wnd[1]/usr/ctxtDY_PATH").setFocus
session.findById("wnd[1]/usr/ctxtDY_PATH").caretPosition = 8
session.findById("wnd[1]/tbar[0]/btn[0]").press```
In your example, you used the time from the previous login. I present an example of real system time.
e.G.
...
session.findById("wnd[0]/mbar/menu[4]/menu[11]").select
myTime = session.findById("wnd[1]/usr/ctxtSYST-UZEIT").text
myName = replace(myTime, ":", "-") & ".xlsx"
session.findById("wnd[1]").close
session.findById("wnd[0]/tbar[1]/btn[43]").press
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = myName
...
Regards, ScriptMan
I have a XLSM that I am writing VBA for. The original workbook has a button that pulls records from a database has the following code involved:
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Sub SAPDownloadAttachment()
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "IW37N"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/tbar[1]/btn[8]").Press
session.FindById("wnd[0]/mbar/menu[0]/menu[6]").Select
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
Worksheets("Sheet1").Range("A:Z").Columns.AutoFit
Worksheets("Sheet1").Range("L:L").Columns.AutoFit
Worksheets("Sheet1").Range("B:B").Columns.AutoFit
Rows(1).Insert shift:=xlShiftDown
Rows(1).Insert shift:=xlShiftDown
Rows(1).Insert shift:=xlShiftDown
Range("A4:Z4").AutoFilter
Selection.HorizontalAlignment = xlCenter
Dim oOLE As Excel.Button
Set oOLE = ActiveSheet.Buttons.Add(Left:=0, Top:=0, Height:=45, Width:=200)
oOLE.Caption = "Return to Original"
oOLE.OnAction = ""
The issue I am having is when the program gathers what it needs to from the database it prints a new XLSX workbook... I need it to print an XLSM so that I can add an action to the button to close the newly printed workbook and return to the original workbook where more buttons will be placed. Is it possible to have a macro workbook print another macro workbook?
I'm trying to connect to SAP through Excel VBA to run a recorded macro.
When it reaches the start of actual SAP code it places the
Run-Time Error '91'
on the line session.FindById("wnd[0]").Maximize.
If I delete that line it has the same issues with every session line.
I verified the references and declarations.
I ran it as a VBS script and it worked.
I'm logged into a SAP session before running the code.
appl contains Nothing in the value of the Local Windows. On the Type it contains GuiApplication.
Here is the snip of the code:
Private Sub CommandButton1_Click()
Dim Tablename As String
If TextBox1 = "" Then
If Not IsObject(appl) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = appl.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject Application, "on"
End If
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "Stock01"
session.FindById("wnd[0]/tbar[0]/btn[0]").press
session.FindById("wnd[0]/usr/ctxtS_MATNR-LOW").Text = "566666"
session.FindById("wnd[0]/usr/ctxtS_MATNR-HIGH").Text = "5666666"
session.FindById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = "1111"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").Text = "1045"
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").SetFocus
session.FindById("wnd[0]/usr/ctxtS_WERKS-HIGH").caretPosition = 4
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").pressToolbarContextButton "&MB_EXPORT"
session.FindById("wnd[0]/usr/cntlYCONTAINER/shellcont/shell").selectContextMenuItem "&XXL"
session.FindById("wnd[1]/tbar[0]/btn[0]").press
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\Documents\SAP_GUI_Code"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "Stock.XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 11
session.FindById("wnd[1]/tbar[0]/btn[11]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
session.FindById("wnd[0]/tbar[0]/btn[3]").press
exit Sub
You say that appl contains Nothing. So it's normal that session also contains Nothing, hence the error 91 when the variable is used.
As #GSerg said in the conversation:
That would be because IsObject determines if the variable has an object type, not whether it contains an instance of an object. In particular, IsObject(Nothing) is True. You want to replace all your IsObject calls with Is Nothing tests.
Consequently, the first lines should be:
If appl Is Nothing Then
Set SapGuiAuto = GetObject("SAPGUI")
Set appl = SapGuiAuto.GetScriptingEngine
End If
If Connection Is Nothing Then
Set Connection = appl.Children(0)
End If
If session Is Nothing Then
Set session = Connection.Children(0)
End If
I tried to run the SAP script recording to gather a text that is displayed for several item numbers automatically. While I was checking it this is the code I got :
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").setFocus
session.findById("wnd[0]/usr/ctxtRC29N-STLAN").caretPosition = 0
session.findById("wnd[0]").sendVKey 4
session.findById("wnd[1]/usr/lbl[1,10]").setFocus
session.findById("wnd[1]/usr/lbl[1,10]").caretPosition = 0
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/ctxtRC29N-MATNR").text = "508546"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").text = "1000"
session.findById("wnd[0]/usr/ctxtRC29N-WERKS").setFocus
session.findById("wnd[0]/tbar[0]/btn[0]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tblSAPLCSDITCALT/ctxtRC29K-STLST[1,15]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCDO").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA").select
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,3]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT").select
session.findById("wnd[0]/tbar[0]/btn[3]").press
session.findById("wnd[0]/usr/tabsTS_ITOV/tabpTCMA/ssubSUBPAGE:SAPLCSDI:0152/tblSAPLCSDITCMAT/txtRC29P-KTEXT[3,6]").setFocus
session.findById("wnd[0]").sendVKey 2
session.findById("wnd[0]/usr/tabsTS_ITEM/tabpPDAT/ssubSUBPAGE:SAPLCSDI:0840/btnRC29P-ICON1").press
session.findById("wnd[0]/usr/cntlSCMSW_CONTAINER_2102/shellcont/shell").setDocument 1,"e1xydGYxXGFkZWZsYW5nMTAyNVxhbnNpXGFuc2ljcGcxMjUyXHVjM="
Obviously, I did not get the required result as I want to retrieve the displayed text and not the text field. Any ideas on how to get that?
First of all instead of .setFocus method try to use .Text method. Then create variable like strMyText. After that assign text from SAP to this variable and at the end paste it somewhere.
So it will be something like this:
Sub SAPText()
Dim strMyText
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").text = "CS03"
session.findById("wnd[0]").sendVKey 0
strMyText = session.findById("wnd[0]/usr/ctxtRC29N-STLAN").Text
Activecell.value = strMyText
End sub