对象'ISapCTextField'的方法'Text'失败

问题描述 投票:1回答:4

我最近一直在研究excel VBA上的一些自动化。该程序从SAP提取数据。然而,总会出现随机发生的错误:“对象'ISapCTextField'的方法'文本'失败”。我已经搜索了很多寻找解决方案,但没有一个解决方案有效。我不知道它是否是excel或activeX或其他一些错误,并且通过尝试多次错误处理也似乎不起作用。因此,我没有尝试更多的方法,而是完全避免使用.Text方法。错误导致行的示例:

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

为了避免使用文本方法,我使用SendKeys来实现同样的目的。基本上将SAP窗口设置为活动窗口,并使用set focus选择SAP中的所需字段,然后使用粘贴Ctrl + v sendkeys将范围中的文本粘贴到字段中。以下是代码:

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

这是蛮力的方式,但它的工作没有错误大声笑。但请提出建议,说明为什么会这样或更好地处理这个问题。祝你好运,祝你有愉快的一天:)

vba excel-vba text methods sap
4个回答
1
投票

我也遇到了同样的情况。我解决了我想那就是你用的句子

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

你应该尝试使用

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

0
投票

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

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

此致,ScriptMan


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

问候,雅各布。


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

© www.soinside.com 2019 - 2024. All rights reserved.