copying data from sap to excel - excel

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

Related

Why does using this closing line of code does not work?

The code stops at the following line working because of the following line
objExcel.Workbooks("cde.XLSX").close
The code properly downloads data from SAP and save it as "cde.xlsx" in a folder location. Once the saving is processed the file opens up by itself and the code is supposed to close it.
The closing step does not to work.
It used to work, did not change anything in the code.
Dim objExcel
Dim objSheet, lastrowRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.Workbooks("Extract_zrep.xlsm").Sheets("Sheet1")
Set lien = objExcel.Workbooks("Extract_zrep.xlsm")
lastrow = objSheet.Range("B65536").End(-4162).Row
SaveToFolder = objSheet.Cells(5, 5).Value
Date1 = Trim(CStr(objSheet.Cells(2, 6).Value))
Date2 = Trim(CStr(objSheet.Cells(2, 8).Value))
session.findById("wnd[0]/tbar[0]/okcd").text = "/nZMM_xxxx"
session.findById("wnd[0]").sendVKey 0
session.findById("wnd[0]/tbar[1]/btn[17]").press
session.findById("wnd[1]/usr/txtV-LOW").text = ""
session.findById("wnd[1]/usr/txtENAME-LOW").text = "USER"
session.findById("wnd[1]/tbar[0]/btn[8]").press
session.findById("wnd[0]/usr/ctxtP_VARI").text = "CDE"
session.findById("wnd[0]/tbar[1]/btn[8]").press
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
session.findById("wnd[1]/tbar[0]/btn[0]").press
session.findById("wnd[1]/usr/ctxtDY_PATH").text = SaveToFolder
session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "cde.XLSX"
session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 3
session.findById("wnd[1]/tbar[0]/btn[0]").press
objExcel.Workbooks("cde.XLSX").close
Edited:
If you know the workbook in discussion full name, as I suppose, please replace that not working code line with the next code:
Dim objEx As Object, wbFullName As String
wbFullName "C:\your real path\cde.XLSX" 'please, use the real folder path!
Set objEx = GetObject(wbFullName).Application
objEx.Workbooks("cde.XLSX").close
objEx.Quit
It will find and close the new Excel Session, where the workbook is open, close the workbook and quit session...

Excel VBA runs step by step but not in whole

I have written the below code to export data from SAP GUI.
If I go step by step everything works fine. Excel is exported and data is copied to the proper place. But if I try to run it NOT step by step it goes on error (subscript out of range).
The problem is at the 'close SAP session part of the code. from that point the code does not run forward if I try to run it at once. I tried to use a timer which stops the code until the exported file opens but it does not help but I am sure that the problem is that the exported file opens too slowly and the code can't run forward.
Do you have any idea how to solve this? Thanks
Sub SapExport_Returns()
Dim wb As Workbook
Dim ws As Worksheet
Dim ws2 As Worksheet
Dim Wbname As String
'SapExport file is open with Cover sheet filled and Data sheet empty
Set wb = Workbooks("SAP export")
Set ws = wb.Worksheets("Cover")
Set ws2 = wb.Worksheets("Data")
'turn off screen update
Application.ScreenUpdating = False
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(SAPConnection) Then
Set SAPConnection = SAPApplication.Children(0)
End If
If Not IsObject(SAPsession) Then
Set SAPsession = SAPConnection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject SAPsession, "on"
WScript.ConnectObject Application, "on"
End If
'Disable the Application Alert before saving the file
Application.DisplayAlerts = False
SAPsession.findById("wnd[0]").maximize
'transaction code
SAPsession.findById("wnd[0]/usr/cntlIMAGE_CONTAINER/shellcont/shell/shellcont[0]/shell").doubleClickNode "F00006"
'company code
SAPsession.findById("wnd[0]/usr/ctxtDD_BUKRS-LOW").Text = CStr(ws.Range("C3").Value)
'open items on date
SAPsession.findById("wnd[0]/usr/ctxtPA_STIDA").Text = ws.Range("H5").Value
'layout
SAPsession.findById("wnd[0]/usr/ctxtPA_VARI").Text = CStr(ws.Range("H8").Value)
SAPsession.findById("wnd[0]/usr/ctxtPA_VARI").SetFocus
SAPsession.findById("wnd[0]/usr/ctxtPA_VARI").caretPosition = 11
SAPsession.findById("wnd[0]/tbar[1]/btn[8]").press
SAPsession.findById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
'workbook naming
Wbname = CStr(ws.Range("C15").Value)
SAPsession.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Wbname
SAPsession.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 8
SAPsession.findById("wnd[1]/tbar[0]/btn[0]").press
'close SAP session
'session.findById("wnd[0]").maximize
'session.findById("wnd[0]/tbar[0]/btn[15]").press
'session.findById("wnd[0]/tbar[0]/btn[15]").press
'Enabling the Application Alerts after saving the file
Application.DisplayAlerts = True
'wait until SAP exported file opens
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 50
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
'copying formatted data to file and close SAP extract
Workbooks(Wbname).Worksheets("Sheet1").Range("A1").CurrentRegion.Copy ws2.Range("A2")
ws2.Range("A2").CurrentRegion.Columns.AutoFit
ActiveWindow.DisplayGridlines = False
ws2.Name = "FBL5N"
wb.Sheets.Add.Name = "Data"
'Turn on screen update
Application.ScreenUpdating = True
End Sub

Using an XLSM to capture and output from a database

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?

Copy / paste dynamic named range from Excel to SAP with VBA

I'm trying to copy a dynamic (named) range of cells from Excel to SAP with the help of a VBA script (manually this would be done through a copy / paste from a clipboard ). The range of values from Excel need to be copied into the multiple selection window of SAP.
But unfortunately it doesn't work and I don't know how to solve it.
This is where I get an error.
Vendors = Sheets("vendors").Range("UniqueVendors").Value
Can anyone help me with this?
Please see here below the code I'm using:
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
Sub SAPDownloadReport()
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Dim CompanyCode As String
Dim ClearingStartDate As Date
Dim ClearingEndDate As Date
Dim SAPLayout As String
Dim FolderPath As String
Dim Filename As String
Dim Vendors As Integer
CompanyCode = Sheets("report").Range("B2").Value
ClearingStartDate = Sheets("report").Range("B3").Value
ClearingEndDate = Sheets("report").Range("B4").Value
SAPLayout = Sheets("report").Range("B5").Value
FolderPath = Sheets("report").Range("B6").Value
Vendors = Sheets("vendors").Range("UniqueVendors").Value
'Insert your SAP Script here
session.FindById("wnd[0]").maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nFBL1N"
session.FindById("wnd[0]").sendVKey 0
session.FindById("wnd[0]/usr/btn%_KD_LIFNR_%_APP_%-VALU_PUSH").press
session.FindById("wnd[1]/usr/tabsTAB_STRIP/tabpSIVA/ssubSCREEN_HEADER:SAPLALDB:3010/tblSAPLALDBSINGLE/ctxtRSCSEL_255-SLOW_I[1,0]").Text = Vendors
session.FindById("wnd[1]").sendVKey 0
session.FindById("wnd[1]/tbar[0]/btn[8]").press
session.FindById("wnd[0]/usr/radX_CLSEL").Select
session.FindById("wnd[0]/usr/ctxtKD_BUKRS-LOW").Text = CompanyCode
session.FindById("wnd[0]/usr/ctxtSO_AUGDT-LOW").SetFocus
session.FindById("wnd[0]/usr/ctxtSO_AUGDT-LOW").caretPosition = 0
session.FindById("wnd[0]").sendVKey 4
session.FindById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = ClearingStartDate
session.FindById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20211001,20211001"
session.FindById("wnd[0]/usr/ctxtSO_AUGDT-HIGH").SetFocus
session.FindById("wnd[0]/usr/ctxtSO_AUGDT-HIGH").caretPosition = 0
session.FindById("wnd[0]").sendVKey 4
session.FindById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").focusDate = ClearingEndDate
session.FindById("wnd[1]/usr/cntlCONTAINER/shellcont/shell").selectionInterval = "20211105,20211105"
session.FindById("wnd[0]/usr/ctxtPA_VARI").Text = SAPLayout
session.FindById("wnd[0]/usr/ctxtPA_VARI").SetFocus
session.FindById("wnd[0]/usr/ctxtPA_VARI").caretPosition = 12
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/mbar/menu[0]/menu[3]/menu[1]").Select
session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = FolderPath
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "EXPORT.XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 5
session.FindById("wnd[1]/tbar[0]/btn[11]").press
MsgBox "Extraction done"
End Sub
(Maybe copying a Named Range isn't the solution maybe copying the Clipboard from Excel or something different is?)
If the Excel data is in the clipboard with the .Copy command, it can be pasted into SAP as follows:
...
session.findById("wnd[0]/usr/btn%_KD_LIFNR_%_APP_%-VALU_PUSH").press
session.findById("wnd[1]/tbar[0]/btn[24]").press
session.findById("wnd[1]/tbar[0]/btn[8]").press
...
Regards, ScriptMan
Don't know the SAP part but instead of copying you are trying to assign your range to an integer. Replace your "vendors = .." line with:
Sheets("vendors").Range("UniqueVendors").Copy
So no need to assign to anything.

Excel file updated but no change: Export query to excel without header

My VBA code is supposed to transfer my query in MS Access (without header)
to cell K17 on an existing excel spreadsheet named Totals.
When I run the module, under the property of the excel file, last modified time did get updated to when I run the module. But I don't see my query on the excel spreadsheet.
Any advice on my code will be highly appreciated. Thank you so much!
Sub TransToXL()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim acRng As Variant
Dim xlRow As Integer
Dim qry As QueryDef
Dim rst As Recordset
Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Users\MyName\Desktop\August 2017.xlsx")
Set xlWS = xlWB.Worksheets("Totals")
xlRow = (xlWS.Columns("K").End(xlDown).Row)
Set qry = CurrentDb.QueryDefs("DollarsSold")
Set rst = qry.OpenRecordset
Dim c As Integer
c = 11
xlRow = xlRow + 16
Do Until rst.EOF
For Each acRng In rst.Fields
xlWS.Cells(xlRow, c).Formula = acRng
c = c + 1
Next acRng
xlRow = xlRow + 1
c = 1
rst.MoveNext
If xlRow > 25 Then GoTo rq_Exit
Loop
rq_Exit:
rst.Close
Set rst = Nothing
Set xlWS = Nothing
xlWB.Close acSaveYes
Set xlWB = Nothing
xlApp.Quit
Set xlApp = Nothing
Exit Sub
End Sub
The variable c is the one that stores colum number, in which c =1 means column A, 11 will be K. In this code, I'm confused about the way I set xlRow+16. xlRow+16 , and c = 11 is not pointing out to K17. Any advise on this? Thank you!
Is there a reason why you are using a Do Until loop? If the recordset only has 1 row of data (the total) it looks like you could remove the loop and just use something like:
xlWS.Range("K17").Value = rst.Fields(0)
This would appear after the "Set rst.." line.

Resources