SAP总结

 

1. 获取session(窗口)
Set SapGuiAuto = GetObject("SAPGUI")  'Get the SAP GUI Scripting object
Set SAPApp = SapGuiAuto.GetScriptingEngine 'Get the currently running SAP GUI
Set SAPCon = SAPApp.Children(0) 'Get the first system that is currently connected
Set session = SAPCon.Children(0) 'Get the first session (window) on that connection

2. 遍历当前excel表
For i = 2 To ActiveSheet.UsedRange.Rows.Count
    
'Exit For
Next i

3. 进入tcode
session.findById("wnd[0]/tbar[0]/okcd").Text = "/nme22n"

4. 回车
session.findById("wnd[0]").sendVKey 0

5. 菜单/按钮/标签页
session.findById("wnd[0]/mbar/menu[3]/menu[7]").Select
session.findbyid("wnd[0]/tbar[1]/btn[21]").press
session.findbyid("wnd[0]/usr").FindByNameEx("TABHDT1", 91).Select

6. 字段
6.1 输入
session.findById("wnd[0]/usr/txtEINE-PEINH").Text = price_per
session.findbyid("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").Key = "1"

6.2 取值
price_per = session.findById("wnd[0]/usr/txtEINE-PEINH").Text

6.3 检测是否可编辑
If session.findbyid("wnd[0]/usr/tblSAPDV70ATC_NAST3/cmbNAST-NACHA[3,0]").changeable = True Then

8. 系统消息
8.1 消息类型:错误E,警告W,消息S
session.findById("wnd[0]/sbar").MessageType
8.2 消息文本
session.findById("wnd[0]/sbar").Text

9. 表格(SAPMV50ATC_LIPS_PICK 是表格名)
9.1 获取表格控件对象
set grid = session.findById("wnd[0]/usr").findByNameEx("SAPMV50ATC_LIPS_PICK", 80)

9.2 总行数
Total_items = session.findById("wnd[0]/usr").findByNameEx("SAPMV50ATC_LIPS_PICK", 80).VerticalScrollbar.Maximum + 1  '
If Total_items = 0 Then Total_items = 1

9.3 每屏(页)行数
PageSize = session.findById("wnd[0]/usr").findByNameEx("SAPMV50ATC_LIPS_PICK", 80).VerticalScrollbar.PageSize

9.4 翻页
For j = 1 To Total_items
	pageindex = (j - deleted_rows) Mod PageSize
	If pageindex = 1 Then
		If j > 1 Then session.findById("wnd[0]/tbar[0]/btn[82]").press   '点翻页按钮
		currentrow = 0
	Else
		currentrow = currentrow + 1
	End If
Next j	
	
9.5 竖向滚动条,需在输入当前行数据后回车并处理警告提示,否则系统自动滚回第0(1)行
Position = session.findbyid("wnd[0]/usr").FindByNameEx("SAPLMEGUITC_1211", 80).verticalScrollbar.Position
session.findbyid("wnd[0]/usr").FindByNameEx("SAPLMEGUITC_1211", 80).verticalScrollbar.Position = Position + 1

9.6 当前行(行号从0开始,录制时产生的[列,行],)
Set cur_row = session.findById("wnd[0]/usr").findByNameEx("SAPMV50ATC_LIPS_PICK", 80).Rows(行号)

9.7 当前格(i是列序号,从0开始,),
cell = cur_row(列序号)
可以取文本值 cell.Text,可以SetFocus等

9.8 报表类 如COOIS shell grid 遍历取数
Set grid = session.findById("wnd[0]/usr/cntlCUSTOM/shellcont/shell/shellcont/shell")
For i = 1 To grid.VisibleRowCount
   Cells(i, 1) = grid.getcellvalue(i - 1, "AUFNR")
   Cells(i, 2) = grid.getcellvalue(i - 1, "MATNR")
Next i

10. 弹窗
10.1 检测
session.ActiveWindow.Name = "wnd[1]"

10.2 提取显示的内容
session.ActiveWindow.PopupDialogText
另一种提取方法
If session.findbyid("wnd[1]").Text Like "Release*" Then 

10.3 关闭
session.findbyid("wnd[1]/").Close

11. 异常处理(试错法)
On Error Resume Next
   session.findById ("wnd[1]/usr/btnSPOP-VAROPTION1")
If Err.Number = 0 Then
	session.findById("wnd[1]/usr/btnSPOP-VAROPTION1").press
End If

12. 提取消息文本中的单据号
If InStr(Result, "has been saved") > 0 Then
	array1 = Split(Result, " ")
	Cells(i, 10) = array1(UBound(array1) - LBound(array1) - 3)     'extract delivery number from status text "FZ delivery 3333330 has been saved"
End If

13. 方法
Function 方法1(参数)
	方法1 = 111
End Function

14. 常见问题
14.1 处理非预期的弹窗与出错,警告,特别是表格行处理时,每一行输入数据后要按回车并处理可能出现的警告提示(回车)
14.2 动态屏幕,
14.3 表格字段的位置(列序号),用名称定位,MIGO,ME2N中每一行都因业务数据(K库存)不同,各列的序号也不一样
有多少个列就设多少个代表列序号的变量,用名称去匹配,需循环每行时都重新获取列序号

'Excel requires that the session number is an integer, so you can use the type conversion from Cint(). 
'Strange that this is advised/required even when SessionNumber is defined as an integer.
'替换下面的表名SAPLMIGOTV_GOITEM、字段名GOITEM-LOGLEVEL及字段名变量col_log_level
Set cur_row = session.findById("wnd[0]/usr").FindByNameEx("SAPLMIGOTV_GOITEM", 80).Rows(0)
col_count = 0 
For ii = 0 To cur_row.Count - 1     'get the col index due to different user settings
	col_count = col_count + 1		'默认找到了列
	If cur_row(ii + 0).Name = "GOITEM-LOGLEVEL" Then '+0 强制转整数以免出Bad index type for collection access
		col_log_level = ii + 0
	ElseIf cur_row(ii + 0).Name = "GOITEM-MAKTX" Then
		col_mat_desc = ii + 0
	Else
		col_count = col_count - 1		'没找到就不计数
	End If
	If col_count >= 2 Then	'全部找到就退出
		Exit For
	End If
Next ii
Set cur_row = Nothing

15. 调试
在弹出出错对话框,点debug按钮,进入代码调试界面,鼠标悬停可显示变量值,也可右键添观察变量,在底下窗口查看变量详情,
特别是对象变量的可用属性,在代码中可使用Debug.Print输出变量内容,在immediate窗口中查看变量输出

16. 常见出错
The method got an invalid argument
输入了字段不支持的值,如超过长度的文本,数值字段输入了文本,下拉选单输入了非选单值(内部只接受key值)

17. 如何动态选择物料主数据中的视图,完整代码请参考MM02那篇文章
        For ii = 0 To 18
                On Error Resume Next  'in case view not yet created/available for change
                    session.findById ("wnd[1]/usr/tblSAPLMGMMTC_VIEW/txtMSICHTAUSW-DYTXT[0," & CStr(ii) & "]")
                If Err.Number <> 0 Then Exit For
                If session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW/txtMSICHTAUSW-DYTXT[0," & CStr(ii) & "]").Text = "General Plant Data / Storage 1" Then
                    session.findById("wnd[1]/usr/tblSAPLMGMMTC_VIEW").getAbsoluteRow(ii).Selected = True  'select Purchase Order Text view
                    Exit For
                End If
            Next ii
posted @ 2023-08-24 22:04  CrossPython  阅读(62)  评论(0编辑  收藏  举报