2

我必须从 SAP 中提取数据。此错误随机发生:

对象“ISapCTextField”的方法“文本”失败

我进行了搜索,但没有一个解决方案有效。多次尝试的错误处理也不起作用。我没有尝试更多的方法,而是.Text完全避免了这种方法。

导致错误的行示例:

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

为了避免使用该.text方法,我曾经SendKeys实现同样的事情。基本上使 SAP 窗口成为活动窗口,并通过使用 set focus 在 SAP GUI 中选择所需的字段,然后使用Ctrl+ Vviasendkeys将文本从范围粘贴到字段。下面是代码:

'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

要调用该函数,只需使用 SAP 脚本记录获取字段 ID 名称并解析为 SAPSafeText(“字段 ID 为字符串”,“单元格范围为字符串”)。

调用示例:

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

这是蛮力的方式,但它有效。

为什么会发生错误?

有没有更好的方法来处理这个?

4

4 回答 4

2

我也遇到了同样的情况。我解决它。我认为那是您使用的句子

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

你应该尝试使用

session.findbyid (*****).text = cells(i,j).value
于 2018-09-05T07:32:44.483 回答
0

有时我可以通过重新启动事务来解决类似的错误。

例如:

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

问候, ScriptMan

于 2018-04-26T10:36:00.693 回答
0

您可以尝试以下方法而不是 sendkeys 方法:

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

问候, ScriptMan

于 2018-04-23T06:25:26.057 回答
0

下面是可能导致随机错误的代码片段。还有大约 7 个其他报告。这是 MRP 报告示例。

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

问候,雅各布。

于 2018-04-24T02:15:38.057 回答