2

I have to pull data from SAP. This error happens randomly:

Method 'Text' of object 'ISapCTextField' failed

I searched but none of the solutions work. Error handling by trying multiple times also didn't work. Instead of trying more methods, I avoided the .Text method altogether.

Example of line causing the error:

session.findById("wnd[0]/usr/ctxtMATNR-LOW").text = "500000000"

To avoid using the .text method, I used SendKeys to achieve the same thing. Basically making the SAP window as active window and selecting the desired field in SAP GUI by using set focus, and then using Ctrl+V via sendkeys to paste the text from a range to the field. Below is the code:

'Declaration
Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function SetForegroundWindow Lib "user32" ( _
ByVal HWnd As Long) As Long


'Finds SAP Window.
Public Sub ActivateSAPWindow()

    Dim HWnd As Long
    'SAP window Name can be found on the status bar of the Portal.
    'Note: This only works in when you click on R/3 and it open a portal. It will not work if it open in the internet explorer
    'To make it work for internet explorer , Simply change the name of the Window to find internet explorer or any window you wish.
    HWnd = FindWindow(vbNullString, "R/3 - SAP NetWeaver Portal - Internet Explorer")
        If HWnd Then
            SetForegroundWindow HWnd
    End If
    
End Sub

Public Sub SAPSafeText(ID As String, OriginCell As String)
    
    'Location of the cell you wanna copy to the field.
    Worksheets("SAP Mapping").Range(OriginCell).Copy
    
    Call ActivateSAPWindow
    Session.FindByID(ID).SetFocus
    
    SendKeys "^v"
    
    'Important to wait for completion before next line.
    Wait (5)
End Sub

To call the function , Simply use SAP script record to get the Field ID name and parse into the SAPSafeText("ID of the Field as string", "Cell Range as string").

Example of call:

Call SAPSafeText("wnd[0]/usr/ctxtBWART-LOW", Low)
Call SAPSafeText("wnd[0]/usr/ctxtBWART-HIGH", High)

This is the brute force way but it works.

Why is the error happening?

Is there a better way to handle this?

Community
  • 1
  • 1
Jacob Tang
  • 21
  • 1
  • 3

4 Answers4

2

I met the same situation too. I solve it. I think that is you use the sentence like

session.findbyid (*****).text = cells(i,j)

you should try to use

session.findbyid (*****).text = cells(i,j).value
piet.t
  • 11,718
  • 21
  • 43
  • 52
李Eatin
  • 21
  • 4
  • I know this is old, but I found it via asearch. I had theexact same problem but now use `.value` and it works without error! – alowflyingpig Jun 05 '19 at 03:58
0

You could try the following instead of sendkeys method:

...
Application.Wait (Now + TimeValue("0:00:01"))
session.findById("wnd[0]/usr/ctxtMATNR-LOW").text = "500000000"
...

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9
  • Hello ScrptMan, Yeah that is one of the first thing i tried but it does not work. I tried Application.wait on all lines but it doesn't work .I also tried many other things like early binding, reinstalling library and excel + SAP etc but all of the methods evenutally run into that error – Jacob Tang Apr 23 '18 at 06:39
  • The thing is that this fails at random intervals it seems, and i have quite a lot of .text. So firstly it will be really messy and secondly it dosn't rly fix it. So i use this as an error handler , the first try use the .text method but if error then use the sendkey. However i still have no idea whats causing it . I looked through the SAP forums and seemed no one rly knows the exact cause ? Regards , Jacob – Jacob Tang Apr 23 '18 at 06:44
  • What is the SAP transaction? Is it possible to see the script / code completely? – ScriptMan Apr 23 '18 at 08:29
  • Well The transactions are Shell custom transactions and some build in ones like MB51,MB52,VL06O . As for the script i’ll Post some time later when I get to work. The problem is there are quite a few people asked for solution for this error and there are so many solutions which I tried many of them to no avail and wasted quite a lot of time ... – Jacob Tang Apr 23 '18 at 10:29
  • And the error is quite hard to debug as it happenes like suddenly after a few runs , and at random lines with .text = “abc” no matter which SAP transactions or Custom transactions. – Jacob Tang Apr 23 '18 at 10:31
  • 1
    Before you take any further steps, you should check the following: Check the LAN connection for the client and ensure its set to High Speed. http://success.panaya.com/Test-Center/Testing-Setup/70591162/How-to-Set-SAP-Network-Settings-to-High-Speed-connection-Client-Side.htm – ScriptMan Apr 23 '18 at 11:07
  • We cannot access any settings in the Login Portal. Our SAP profiles are very restricted and one of the settings except customization could be viewed or changed Regards, Jacob – Jacob Tang Apr 24 '18 at 01:57
0

below are snips of the code that could cause the random error. There are about 7 other Reports. Here is the MRP report example.

Public SapGuiAuto As Object
Public SAPApp As SAPFEWSELib.GuiApplication
Public SAPConnection As SAPFEWSELib.GuiConnection
Public Session As SAPFEWSELib.GuiSession

Sub InitSession()

    On Error GoTo InternetAutomation
    ErrorCounter = ErrorCounter + 1

    Set SapGuiAuto = GetObject("SAPGUI")
    If Not IsObject(SapGuiAuto) Then
        Exit Sub
    End If

    Set SAPApp = SapGuiAuto.GetScriptingEngine()
    If Not IsObject(SAPApp) Then
        Exit Sub
    End If

    Set SAPConnection = SAPApp.Connections(0)
    If Not IsObject(SAPConnection) Then
        Exit Sub
    End If

    Set Session = SAPConnection.Sessions(0)
    If Not IsObject(Session) Then
        Exit Sub
    End If
Exit Sub
InternetAutomation:
.........
End sub

sub MRP()
    Call InitSession

    Call TCodeBox("/n/DS1/APO_C_")
    Call PlantCode_MRP("A11")
    Call Material_MRP("E3")
    Call SetPath_MRP            
    Call Execute

    Call MRPReportProcess

End Sub

Sub PlantCode_MRP(Cell As String)

    session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = Range(Cell)
    session.findById("wnd[0]/usr/btn%_S_WERKS_%_APP_%-VALU_PUSH").press
    Call SAPMultiSelect(Cell)
End Sub

Sub Material_MRP(Cell As String)

    Worksheets("MB52 Total").Activate
    session.findById("wnd[0]/usr/btn%_S_MATNR_%_APP_%-VALU_PUSH").press
    Call SAPMultiSelect(Cell)
End Sub

Sub SetPath_MRP()

    session.findById("wnd[0]/usr/ctxtP_PATH").Text = Desktop
    session.findById("wnd[0]/usr/txtP_NAME").Text = MRPFileName
End Sub

Sub TCodeBox(TCode As String)

    session.findById("wnd[0]/tbar[0]/okcd").Text = TCode
    On Error GoTo TCodeErrorHandler
    session.findById("wnd[0]").sendVKey 0

TCodeErrorHandler:

    session.findById("wnd[0]/tbar[0]/btn[15]").press
    session.findById("wnd[0]/tbar[0]/okcd").Text = TCode
    session.findById("wnd[0]").sendVKey 0
    Resume Next
    Exit Sub 'Enter

End Sub

Sub Execute()

    session.findById("wnd[0]/tbar[1]/btn[8]").press
End Sub

Regards,Jacob.

Jacob Tang
  • 21
  • 1
  • 3
0

Sometimes I could solve similar errors by restarting the transaction.

for example:

Sub PlantCode_MRP(Cell As String)
 on error resume next
 session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = Range(Cell)
 if err.number <> 0 then
   Call TCodeBox("/n/DS1/APO_C_")
   session.findById("wnd[0]/usr/ctxtS_WERKS-LOW").Text = Range(Cell)
 end if 
 on error goto 0
 'On Error GoTo InternetAutomation
 session.findById("wnd[0]/usr/btn%_S_WERKS_%_APP_%-VALU_PUSH").press
 Call SAPMultiSelect(Cell)
End Sub

Regards, ScriptMan

ScriptMan
  • 1,580
  • 1
  • 9
  • 9
  • Scriptman thanks for your reply , really appreciated it. I’ve did something similar basically on error calls the entire sub again which exits and enters the transaction again , and give it max of 5 tries , however when ever the error happens , it always exceed the max 5 tries .... still puzzles why it’s happening :( – Jacob Tang Apr 27 '18 at 00:56
  • I experienced a similar case in which I was able to loop a maximum of 50 times. After that, there was always a mistake. My opinion: A script does not do anything like a human. If a human were just as fast as a script, it would come to the error state too. Therefore, it is worth thinking about it. It's an error that happens at a certain speed of processing in SAP. Finished! – ScriptMan Apr 27 '18 at 05:52
  • You're Probably right :) Its probably some bug or something , well at least it works now with work around.. Thanks for your time ScriptMan. – Jacob Tang Apr 30 '18 at 03:55