I need to populate dates on fields in SAP which if manually entered is properly captured by the script recorder.
Is it possible to update the script dates using a cell link in Excel?
session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = "2"
session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = "2"
session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = "2016"
session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = "29.02.2016"
I plan to copy the recorded SAP script and incorporate it in an Excel macro as a button.
You may try like this:
Set app = CreateObject("Excel.Application")
Set wbook = app.Workbooks.Open("c:\tmp\prices.xls")
set sheet = wbook.Sheets("Tabelle1")
session.findById("wnd[0]/usr/ctxtLKO74-PERIO").Text = sheet.Cells(1,2).Value
session.findById("wnd[0]/usr/ctxtLKO74-BUPERIO").Text = sheet.Cells(1,3).Value
session.findById("wnd[0]/usr/txtLKO74-GJAHR").Text = sheet.Cells(1,4).Value
session.findById("wnd[0]/usr/ctxtLKO74-BZDAT").Text = sheet.Cells(1,5).Value
Get App object, then get workbook and worksheet objects, and then assign your script cell values.
Everything is well discussed here including all the comments asking for different scenarios. https://scn.sap.com/thread/1699675
Related
I'm using Microsoft.Office.Interop.Excel to write an Excel file. The code below is not working:
var excel = new Microsoft.Office.Interop.Excel.Application();
var workbook = excel.Workbooks.Add(Type.Missing);
var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
worksheet.Name = "sheet1";
worksheet.Cells[1,1] = "top left";
worksheet.Cells[1,2] = "top right";
worksheet.Cells[2,1] = "bottom left";
worksheet.Cells[2,2] = "bottom right";
workbook.SaveAs("temp.xlsx");
workbook.Close();
excel.Quit();
It produces an Excel file but it is empty. I'm expecting to see the text "top left", "top right",... in the first 2 by 2 cells. But I see nothing.
Why is it not writing content to my worksheet?
You can find my code at github: https://github.com/gibran-shah/Image2Excel
Perhaps this link might help you: http://csharp.net-informations.com/excel/csharp-create-excel.htm
According to the link above, try using:
var worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.get_Item(1);
To troubleshoot further, I would choose some other name for the sheet, and check that the name is actually being set by opening the workbook and observing the name in the sheet's tab. This would ensure you are indeed adding data to the sheet that you think you are.
I am trying to adjust some workbook audit code for the new threaded comments. Basically the code would cycle through all workbook comments (notes) and consolidate them into a new sheet at the end of the workbook.
With what is now notes, I use the following (noting cmt1 is declared as a comment object and wsSource is a worksheet object)
For Each cmt1 In wsSource.Comments
'Collect comment data for testing/pasting
strSheetname = wsSource.Name
strCmt = cmt1.Text
strCellref = cmt1.Parent.Address
strContent = cmt1.Parent.Formula
etc.
Now with threaded comments, the same type of approach didn't work. I am trying to do a numerical loop using a counter and having code like the below:
For x = 1 to wsSource.commentsthreaded.count
However the count property doesn't work. According to the nmicrosoft website, it should be one of the 4 available properties.
https://learn.microsoft.com/en-us/office/vba/api/excel.commentsthreaded
Does anyone else have this issue? Any way to fix it?
Issue resolved - it looks like the problem was the Excel version. I was running 1904 and it now works since updating to 1906.
I need to know how to connect Excel with SAP using RFC. I have not managed to import any SAP data to Excel using the codes found so far.
I would like to be able to import data from any known transaction (e.g. a bill of materials from transaction CO03). From this I would try to understand how to extract other type of tables.
My goal is to be able to import any SAP data on a Excel spreadsheet using RFC. That would be a good start.
Do I need a special SAP account? How to verify my account is enabled to perform this type of tasks?
It is not possible to call any standard transaction remotely as most of them are legacy-like and doesn't return anything directly.
There are couple of ways to fetch data from any transaction but they are out of the scope of this question. The most practical way of retrieveing data from SAP to Excel is to find proper BAPI or remote-enabled FM, (including writing own wrapper FM) and this is the way I gonna describe here.
You don't need special account, you just need to have proper authorizations for RFC-calls, which mainly comprise of S_RFC authorization object
If you use BAPI, you can omit this point. If you created own wrapper, then you have to assure it is remote-enabled.
And then you can call your FM in VBA code and return results to Excel book. Here is the sample code:
' Logging in
Dim retcd As Boolean
Dim SilentLogon As Boolean
Set LogonControl = CreateObject("SAP.LogonControl.1")
Set objBAPIControl = CreateObject("SAP.Functions")
Set R3Connection = LogonControl.NewConnection
R3Connection.Client = "700"
R3Connection.ApplicationServer = "server_address"
R3Connection.Language = "EN"
R3Connection.User = "sap_user"
R3Connection.Password = "sap_pass"
R3Connection.System = "system_id"
R3Connection.SystemNumber = "sys_num"
R3Connection.UseSAPLogonIni = False
retcd = R3Connection.Logon(0, SilentLogon)
If retcd <> True Then MsgBox "Logon failed": Exit Sub
' Declaring FM interface
objBAPIControl.Connection = R3Connection
Set objgetaddress = objBAPIControl.Add("ZNM_GET_EMPLOYEE_DETAILS")
Set objkunnr = objgetaddress.Tables("ET_KUNNR")
Set objaddress = objgetaddress.Tables("ET_CUST_LIST")
' Filling select-options values table from sheet
Dim sht As Worksheet
Set sht = ThisWorkbook.ActiveSheet
If sht.Cells(6, 2).Value <> " " Then
objkunnr.Rows.Add
objkunnr.Value(1, "SIGN") = sht.Cells(6, 2).Value
objkunnr.Value(1, "OPTION") = sht.Cells(6, 3).Value
objkunnr.Value(1, "LOW") = sht.Cells(6, 4).Value
objkunnr.Value(1, "HIGH") = sht.Cells(6, 5).Value
R3Connection.Logoff
P.S. For all this to work in your VBA project you should add references to SAP ActiveX controls, which are located in %ProgramFiles%\SAP\FronEnd\SAPgui directory:
wdtaocxU.ocx
wdtfuncU.ocx
wdtlogU.ocx
wdobapiU.ocx
So references list of your VBA project should look like this
Additionally to Excel solution you may try this open source MS Access application I created long time ago and used many times:
https://blogs.sap.com/2013/08/16/read-data-from-sap-tables-into-ms-access-2003-database/
I want to write a set of 100 select queries in DB2 10.1 to return all rows in each table in the database and have the results exported to an excel spreadsheet with a new tab for each result set.
Is this possible and if so how can I do it?
At the moment the only way I can do this looks like to export each result set and then manually create the multi tabbed spreadsheet by copying each tab across.
Thanks
You can use EasyXLS API for Excel with scripting languages like VB Script.
The VBS code should be similar with this one:
'The class that exports result set to Excel file
Set xls = CreateObject("EasyXLS.ExcelDocument")
' The class used to format the cells
Dim xlsAutoFormat
set xlsAutoFormat = CreateObject("EasyXLS.ExcelAutoFormat")
xlsAutoFormat.InitAs(AUTOFORMAT_EASYXLS1)
For query = 1 To 100
' Add a new sheet
xls.easy_addWorksheet_2("Sheet" & query)
set xlsSheet = xls.easy_getSheetAt(query - 1)
' Create the record set object
Dim objResultSet
Set objResultSet = CreateObject("ADODB.Recordset")
objResultSet.Open queryString, objDBConnection
' Create the list that will store the values of the result set
Dim lstRows
Set lstRows = CreateObject("EasyXLS.Util.List")
' Add the header to the list
Dim lstHeaderRow
Set lstHeaderRow = CreateObject("EasyXLS.Util.List")
lstHeaderRow.addElement("Column 1")
lstHeaderRow.addElement("Column 2")
lstHeaderRow.addElement("Column 3")
lstRows.addElement(lstHeaderRow)
' Add the values from the database to the list
Do Until objResultSet.EOF = True
set RowList = CreateObject("EasyXLS.Util.List")
RowList.addElement("" & objResultSet("Column 1"))
RowList.addElement("" & objResultSet("Column 2"))
RowList.addElement("" & objResultSet("Column 3"))
lstRows.addElement(RowList)
' Move to the next record
objResultSet.MoveNext
Loop
xlsSheet.easy_insertList_2 lstRows, xlsAutoFormat
Next
' Export result sets to Excel file
xls.easy_WriteXLSFile("c:\Result sets.xls")
Check also this link about exporting lists of data to Excel.
If you will choose a non scripting language, the API has methods that insert data directly from the result set and the lists can be skiped. Check another sample here.
i am writting a code to copy a sheet in excel from one workbook to another and I'm having the following issue :
Set xlsAppSource = CreateObject("Excel.Application")
Set xlsWBSource = xlsAppSource.Workbooks.Open(strWBSource)
Set xlsAppCible = CreateObject("Excel.Application")
Set xlsWBCible = xlsAppCible.Workbooks.Open(strWBCible)
xlsWBSource.Sheets(1).Copy before:= xlsWBSource.Sheets(1)
And I get ERROR 1004.
I know the error comes from :
xlsWBSource.Sheets(1) and xlsWBSource.Sheets(1)
because when change the last statement to :
xlsWBSource.activate
ActiveWorkbook.Sheets(strSheetSource).Copy before:= [any working sheet]
it works.
But As I cannot activate the 2 workbooks it's not a way to solve my problem.
Any ideas why it doesnt work ?
thanks
If you are tring to copy from from xlsWBSource to xlsWBCible then you should only use one instance of Excel , you are currently opening two separate versions with your two CreateObjects - the two separate instances can't "talk", hence the error
Whereas this will work:
Set xlsAppSource = CreateObject("Excel.Application")
Set xlsWBSource = xlsAppSource.Workbooks.Open(strWbsource)
Set xlsWBcible = xlsAppSource.Workbooks.Open(strWBcible)
xlsWBSource.Sheets(1).Copy before:=xlsWBcible.Sheets(1)