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?

5 Answers 5

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
1
  • 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! Jun 5, 2019 at 3: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

7
  • 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, 2018 at 6: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, 2018 at 6:44
  • What is the SAP transaction? Is it possible to see the script / code completely?
    – ScriptMan
    Apr 23, 2018 at 8: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, 2018 at 10:29
  • 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. success.panaya.com/Test-Center/Testing-Setup/70591162/…
    – ScriptMan
    Apr 23, 2018 at 11:07
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.

1
  • I also tried using .Text = Worksheets("Sheet").Range(RNG) also just .Text = "999"
    – Jacob Tang
    Apr 24, 2018 at 2:16
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

3
  • 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, 2018 at 0: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, 2018 at 5: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, 2018 at 3:55
0

using cel.text instead of cel.value has solved the error i was facing.

For each cel in Rng.cells session.findById("wnd[0]/usr/ctxtMATNR-LOW").text = cel.Text

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 16, 2023 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.