Format string based on current date and time - excel

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

Related

SAP Script using loop

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

SAP GUI Scripting in Excel VBA - Error 91 (Object Variable...)

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

How to vary data using SAP and vba?

First, I was able to connect SAP GUI to vba. I managed to get a script that works and that sends me an automatic transaction. I would like to know how to make the data entered on SAP for example the date is variable because the date I have to change it every year?
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]/usr/tabsTABSTRIP_TABBL1/
tabpUCOM1/ssub%_SUBSCREEN_TABBL1:RFBILA00:0001/txtBILBJAHR").text = "2015"
For example, I would like the "2015" date to be variable, I would like every month I can change this date to put another one without having to start the extraction on SAP GUI again.
Like this, you should also indent your code:
Option Explicit
Sub Test()
Dim MyYear As String
MyYear = Format(Date, "yyyy")
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]/usr/tabsTABSTRIP_TABBL1/tabpUCOM1/ssub%_SUBSCREEN_TABBL1:RFBILA00:0001/txtBILBJAHR").Text = MyYear
End Sub

Retreive Displayed text field in SAP through macro with Excel

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

VBA to call SAP transaction

I am trying to call a SAP transaction from Excel, I use many codes from the internet but still a failure.
Doesn't matter what code I use, I always get same error message as
A script is opening a connection to system SYSTEM
than error saying:
Runtime error 1000 Error description not avialable"
Than after debug it highlights below in yellow
Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)
its been doing this with all code I tried to pull this.
Code
Dim sap As Object
Dim conn As Object
Sub T_login()
Set sap = CreateObject("SAP.Functions")
Set conn = sap.Connection
conn.System = "SYSTEM"
conn.client = "603"
conn.user = "LANAX001"
conn.Password = "alamzeb4"
conn.Language = "EN"
If conn.logon(0, False) Then
MsgBox "Logon to the SAP system is not possible", vbOKOnly, "Comment"
Else
End If
If Not IsObject(SapGuiApp) Then
Set SapGuiApp = CreateObject("Sapgui.ScriptingCtrl.1")
End If
If Not IsObject(Connection) Then
Set Connection = SapGuiApp.OpenConnection("SYSTEM", True)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
'launch a transaction
session.findById("wnd[0]").Maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "FS10N"
session.findById("wnd[0]").sendVKey 0
End Sub
This is what I use to load data to SAP - verified to work. HTH.
If Not IsObject(App) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set App = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = App.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 App, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nXXXX" 'XXX = your t-code
... ...
Afterwards, it depends on what you do in the t-code.
Good luck!

Resources