excel 对象“ISApCTextField”的方法“Text”失败

roejwanj  于 2023-10-22  发布在  其他
关注(0)|答案(5)|浏览(130)

我得从SAP调出数据此错误随机发生:
对象“ISApCTextField”的方法“Text”失败
我找过了,但没有一个解决方案有效。通过多次尝试进行错误处理也不起作用。我没有尝试更多的方法,而是完全避免了.Text方法。
导致错误的行示例:

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

为了避免使用.text方法,我使用了SendKeys来实现同样的功能。基本上使SAP窗口作为活动窗口,并选择所需的领域在SAP GUI中使用设置焦点,然后使用Ctrl+V通过sendkeys粘贴文本从一个范围到该领域。下面是代码:

'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)

这是蛮力的方式,但它的工作。
为什么会出现错误?
有没有更好的方法来处理这件事?

km0tfn4u

km0tfn4u1#

我也遇到了同样的情况。我来解决。我觉得你用的句子

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

你应该试着用

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

3z6pesqy2#

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

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

问候,ScriptMan

l5tcr1uw

l5tcr1uw3#

下面是可能导致随机错误的代码片段。另有约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

你好,雅各布。

rqmkfv5c

rqmkfv5c4#

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

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

mnemlml8

mnemlml85#

使用cel.text代替cel.value解决了我面临的错误。
对于Rng.cells session.findById(“wnd[0]/usr/ctxtMATNR-LOW”).text = cel.Text中的每个cel

相关问题