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.
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'm trying to build an Excel VBA script that runs through an Excel sheet and extracts reports from a SAP system and puts them in a specific folder.
The parameters start at A10 and B10 and these are put in the SAP screen fields where the report is created and downloaded in the folder on my desktop.
Please see here below an example of the Excel sheet and a screenshot of the SAP screen and the code I'm using.
The script works fine for the first parameters A10 and B10 (it extracts a report from the SAP system and puts it in the desired folder) however I would like to loop the script to extract reports automatically for the other parameters below A10 and B10 and beyond (Loop Until ActiveCell.Value = "")
Can someone help me with this? Where and how do I put the loop?
The code:
'Declaring variables for sub procedures
Option Explicit
Public SapGuiAuto, WScript, msgcol
Public objGui As GuiApplication
Public objConn As GuiConnection
Public session As GuiSession
'Creating sub procedure
Sub SAPCustomerReport()
'Pointing object variables to SAP session
Set SapGuiAuto = GetObject("SAPGUI")
Set objGui = SapGuiAuto.GetScriptingEngine
Set objConn = objGui.Children(0)
Set session = objConn.Children(0)
Dim Vendor As String
Dim CoCo As String
Dim FolderPath As String
Dim SAPOutputLayout As String
Vendor = ActiveWorkbook.ActiveSheet.Range("A10")
CoCo = ActiveWorkbook.ActiveSheet.Range("B10")
FolderPath = ActiveWorkbook.ActiveSheet.Range("B3")
SAPOutputLayout = ActiveWorkbook.ActiveSheet.Range("B4")
'Recorded 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/chkX_SHBV").Selected = True
session.FindById("wnd[0]/usr/chkX_MERK").Selected = True
session.FindById("wnd[0]/usr/chkX_PARK").Selected = True
session.FindById("wnd[0]/usr/ctxtKD_LIFNR-LOW").Text = Vendor
session.FindById("wnd[0]/usr/ctxtKD_BUKRS-LOW").Text = CoCo
session.FindById("wnd[0]/usr/ctxtPA_VARI").Text = SAPOutputLayout
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 = Vendor & CoCo & ".XLSX"
session.FindById("wnd[1]/usr/ctxtDY_FILENAME").CaretPosition = 4
session.FindById("wnd[1]/tbar[0]/btn[11]").Press
MsgBox "Script Completed."
End Sub
A solution could look like this:
Sub SAPCustomerReport()
...
Recorded SAP Script here
i = 10
do
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nFBL1N"
...
session.FindById("wnd[1]/tbar[0]/btn[11]").Press
i = i + 1
Vendor = ActiveWorkbook.ActiveSheet.Range("A" & cstr(i))
CoCo = ActiveWorkbook.ActiveSheet.Range("B" & cstr(i))
loop until Vendor = ""
MsgBox "Script Completed."
End Sub
Regards, ScriptMan
You should split the code in a routine that iterates over the configuration and one that takes as input vendor and CoCo and creates the report.
Furthermore I suggest to format your configuration (A9:B12) as a table (Ribbon: Insert > table) then you can use the listobject in VBA - which is much easier to handle regarding ranges etc. I named the table "tblSAPReports".
Option Explicit
Sub createAllReports()
Dim lo As ListObject
Set lo = ThisWorkbook.Worksheets("Table1").ListObjects("tblSAPReports") 'adjust to your needs
Dim lr As ListRow
Dim Vendor As String, CoCo As String
For Each lr In lo.ListRows
Vendor = lr.Range(1, 1): CoCo = lr.Range(1, 2)
SAPCustomerReport Vendor, CoCo
Next
End Sub
Public Sub SAPCustomerReport(Vendor As String, CoCo As String)
'your code
'but remove Vendor and CoCo variable and the setting of the variables
End Sub
I am pasting some excel data into powerpoint as a picture and I am having some issues. I have 290 files which I am pasting a table into slide 4, 5 and 6 of each PP file. This worked perfectly yesterday when I was only doing 1 table into slide 6. I have replicated the process and now I keep getting random errors at random times. Sometimes its file 10, others file 50, different everytime. The errors range from the paste datatype is not available OR the clipboard is empty. I have tried every datatype, pasting as a metafile, as a shape, as a picture, just basic pasting and nothing stops the error. I have no idea! Here is my code: PLEASE HELP !
Sub Update_Site_Report()
'Initiate Variables
Dim objPPT As Object
Dim PPTPrez As Object
Dim FinSlide As Object
Dim AssumSlide As Object
Dim RiskSlide As Object
Dim FinTable As Object
Dim AssumTable As Object
Dim RiskTable As Object
Dim fileNameString As String
Dim PicCount As Long
Dim PicCount1 As Long
Dim PicCount2 As Long
Dim i As Long
Dim fileN As String
Dim Directory As String
'Create and open powerpoint application
Set objPPT = CreateObject("PowerPoint.Application")
objPPT.Visible = True
Application.ScreenUpdating = False
'Update site report table from spreadsheet
For i = 2 To 291
Sheet20.Cells(18, 2) = Sheet20.Cells(5, i)
Sheet20.Cells(19, 2) = Sheet20.Cells(6, i)
Sheet20.Cells(20, 2) = Sheet20.Cells(7, i)
Sheet20.Cells(21, 2) = Sheet20.Cells(8, i)
Sheet20.Cells(18, 3) = Sheet20.Cells(10, i)
Sheet20.Cells(19, 3) = Sheet20.Cells(11, i)
Sheet20.Cells(20, 3) = Sheet20.Cells(12, i)
Sheet20.Cells(21, 3) = Sheet20.Cells(13, i)
'Take column header from spreadsheet and set as filename
fileN = Sheet20.Cells(4, i)
' Allow directory to be set in excel tab
Directory = Sheet20.Cells(18, 5)
'Open powerpoint presentation at Directory with Filename
Set PPTPrez = objPPT.Presentations.Open(Directory & fileN & ".pptx")
'Set range for site report table
Set Financials = Sheet20.Range("A17:C21")
Set Assumptions = Sheet45.Range("A1:C7")
Set Risks = Sheet45.Range("A24:D41")
'Choose which slide to paste site report table
Set FinSlide = PPTPrez.Slides(6)
Set AssumSlide = PPTPrez.Slides(4)
Set RiskSlide = PPTPrez.Slides(5)
'If there is a table in powerpoint slide, delete the table
For PicCount1 = AssumSlide.Shapes.Count To 1 Step -1
If AssumSlide.Shapes(PicCount1).Type = msoPicture Then
AssumSlide.Shapes(PicCount1).Delete
End If
Next
For PicCount = FinSlide.Shapes.Count To 1 Step -1
If FinSlide.Shapes(PicCount).Type = msoPicture Then
FinSlide.Shapes(PicCount).Delete
End If
Next
For PicCount2 = RiskSlide.Shapes.Count To 1 Step -1
If RiskSlide.Shapes(PicCount2).Type = msoPicture Then
RiskSlide.Shapes(PicCount2).Delete
Debug.Print
End If
Next
'Paste the site report table into the site report
Financials.Copy
FinSlide.Shapes.PasteSpecial ppPasteShape
Set FinTable = FinSlide.Shapes(FinSlide.Shapes.Count)
Assumptions.Copy
AssumSlide.Shapes.PasteSpecial ppPasteShape
Set AssumTable = AssumSlide.Shapes(AssumSlide.Shapes.Count)
Risks.Copy
RiskSlide.Shapes.PasteSpecial ppPasteShape
Set RiskTable = RiskSlide.Shapes(RiskSlide.Shapes.Count)
'Set position of site report table in powerpoint
FinTable.Left = 36
FinTable.Top = 175
FinTable.Width = 614
AssumTable.Left = 36
AssumTable.Top = 80.8
RiskTable.Left = 36
RiskTable.Top = 80.8
RiskTable.Width = 641.5
'Set filename as string
fileNameString = Directory & fileN & ".pptx"
'Save file as filename
PPTPrez.SaveAs fileNameString
'Close powerpoint presentation
PPTPrez.Close
'Repeat for every site (column) - increment i
Next i
'quit powerpoint
objPPT.Quit
Application.ScreenUpdating = True
MsgBox ("Update complete, click ok to exit powerpoint")
End Sub
Disabling Windows clipboard history solves this issue.
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.
How do we create a timeline diagram in Visio using VBA scripts?
I am trying to do this from excel. I have written the VBA script to open the visio diagram and create a basic shape. I want to create a timeline diagram.
Similar to basic_u.vss and visMSDefault, are there any parameters that are required while creating a timeline diagram?
Below is the code snippet that I am using.
Option Explicit
Sub VisioFromExcel()
Dim AppVisio As Object
Dim vsoCharacters1 As Visio.Characters
Dim lX As Long
Dim dXPos As Double
Dim dYPos As Double
'Const visSectionCharacter = 3
'Const visCharacterSize = 7
Set AppVisio = CreateObject("visio.application")
'Set AppVisio = CreateObject("VisioTimelineVBA")
AppVisio.Visible = True
AppVisio.Documents.AddEx "", visMSDefault, 0 'Open Blank Visio Document
'AppVisio.Documents.AddEx "", visMSDefault, 0 'Open Blank Visio Document
AppVisio.Documents.OpenEx "basic_u.vss", visOpenRO + visOpenDocked 'Add Basic Stencil
dXPos = AppVisio.ActivePage.PageSheet.Cells("PageWidth") / 2
dYPos = AppVisio.ActivePage.PageSheet.Cells("PageHeight") / 2
For lX = 1 To Cells(Rows.Count, 1).End(xlUp).Row
AppVisio.Windows.ItemEx(1).Activate
AppVisio.ActiveWindow.Page.Drop AppVisio.Documents.Item("BASIC_U.VSS").Masters.ItemU("Square"), dXPos, dYPos
Set vsoCharacters1 = AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lX).Characters
vsoCharacters1.Begin = 0
vsoCharacters1.End = 0
vsoCharacters1.Text = CStr(Cells(lX, 1).Value)
AppVisio.ActiveWindow.Page.Shapes.ItemFromID(lX).CellsSRC(visSectionCharacter, 0, visCharacterSize).FormulaU = "36 pt"
Next
Set AppVisio = Nothing
End Sub
The below code worked for me.
Option Explicit
Sub VisioFromExcel()
Dim AppVisio As Object
Dim docsObj As Visio.Documents
Dim DocObj As Visio.Document
Dim pagsObj As Visio.Pages
Set AppVisio = CreateObject("visio.application")
AppVisio.Visible = True
Set docsObj = AppVisio.Documents
Set DocObj = docsObj.Add("Timeline.vst")
Set pagsObj = AppVisio.ActiveDocument.Pages
Set AppVisio = Nothing
End Sub