对象"ISapCTextField"的方法"文本"失败



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

对象"ISapCTextField"的方法"文本"失败

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

导致错误的行示例:

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

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

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

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

为什么会发生错误?

有没有更好的方法来解决这个问题?

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

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

你应该尝试使用

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

您可以尝试以下方法而不是发送密钥方法:

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

问候 脚本人

下面是可能导致随机错误的代码片段。还有大约 7 份其他报告。下面是物料需求计划报表示例。

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

问候,雅各布。

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

例如:

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

问候 脚本人

最新更新