1

I am trying to use Python for SAP Scripting, specifically to select a row in a GuiGridView object. It works just fine in VB Script, but in Python I get an exception: -2147352562, 'Invalid number of parameters.', which doesn't make sense to me since I'm using the same number of parameters as for VB Script.

I am trying to get away from VB Script since it doesn't have proper error handling.

Here is the documentation (https://help.sap.com/doc/9215986e54174174854b0af6bb14305a/760.01/en-US/sap_gui_scripting_api_761.pdf ), but even with that I haven't been able to resolve this:

Page 128

SelectedRows (Read-write)

Public Property SelectedRows As String

The string is a comma separated list of row index numbers or index ranges, such as “1,2,4-8,10”.Setting this property to an invalid string or a string containing invalid row indices will raise an exception.

The following VB Script code selects the GuiGridRow as expected, but when I attempt to convert that to Python it won't work.

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

Set GridView = session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell")
GridView.SelectedRows = 1

Here is my Python 3.9 attempt using Jupyter Notebook and the results:

+*In[1]:*+
import win32com.client


+*In[2]:*+
SapGuiAuto = win32com.client.GetObject('SAPGUI')
application = SapGuiAuto.GetScriptingEngine
connection = application.Children(0)
session = connection.Children(0)
type(session)

+*Out[2]:*+
----win32com.client.CDispatch----


+*In[3]:*+
grid_view = session.findById('wnd[0]/usr/cntlGRID1/shellcont/shell')
print(grid_view)

+*Out[3]:*+
<COMObject <unknown>>


+*In[4]:*+
# to show that grid_view is a valid GuiGridView object
grid_view.RowCount

+*Out[4]:*+
----2----


+*In[5]:*+
# reading the SelectedRows works
grid_view.SelectedRows


+*Out[5]:*+
----'1'----


+*In[6]:*+
# setting SelectedRows as number fails
# note: I had manually unselected the row in SAP before running the rest of the code
grid_view.SelectedRows = 1

+*Out[6]:*+

    ---------------------------------------------------------------------------
    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp\20/ipykernel_21344/1273355717.py in <module>
    ----> 1 grid_view.SelectedRows = 1
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)
----


+*In[7]:*+
# setting SelectedRows as string fails
grid_view.SelectedRows = '1'

+*Out[7]:*+

    ---------------------------------------------------------------------------

    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp\20/ipykernel_21344/1222153478.py in <module>
    ----> 1 grid_view.SelectedRows = '1'
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)

----


+*In[8]:*+
# setting SelectedRows as string with multiple rows fails
grid_view.SelectedRows = '0,1'
----

+*Out[8]:*+

    ---------------------------------------------------------------------------

    com_error                                 Traceback (most recent call last)

    ~\AppData\Local\Temp\20/ipykernel_21344/3316141255.py in <module>
    ----> 1 grid_view.SelectedRows = '0,1'
    

    E:\Anaconda\Lib\site-packages\win32com\client\dynamic.py in __setattr__(self, attr, value)
        541                                 entry = self._olerepr_.propMap[attr]
        542                                 invoke_type = _GetDescInvokeType(entry, pythoncom.INVOKE_PROPERTYPUT)
    --> 543                                 self._oleobj_.Invoke(entry.dispid, 0, invoke_type, 0, value)
        544                                 return
        545                         # Check the specific "put" map.
    

    com_error: (-2147352562, 'Invalid number of parameters.', None, None)

----


+*In[9]:*+
# setting SelectedRows as string with multiple rows in a method-type notation fails
grid_view.SelectedRows('0,1')
----

+*Out[9]:*+

    ---------------------------------------------------------------------------

    TypeError                                 Traceback (most recent call last)

    ~\AppData\Local\Temp\20/ipykernel_21344/2186236324.py in <module>
    ----> 1 grid_view.SelectedRows('0,1')
    

    TypeError: 'str' object is not callable

----

Auto
  • 21
  • 4

1 Answers1

1

Ok, after digging around a lot more, patching to the latest version of win32com worked: https://github.com/mhammond/pywin32/tree/main/com/win32com

Once I did that all of the following worked to select rows:

grid_view.SelectedRows = 1

grid_view.SelectedRows = '1'

grid_view.SelectedRows = '0,1'

grid_view.SelectedRows = '0-1'

The following did not work (type error), which makes sense since it would imply tuple packing/unpacking:

grid_view.SelectedRows = 0,1

This results in unexpected rows being selected since it is performing subtraction rather than indicating a range of rows:

grid_view.SelectedRows = 0-1
Auto
  • 21
  • 4