当前位置:网站首页>VBA驱动SAP GUI完成界面元素值初始化
VBA驱动SAP GUI完成界面元素值初始化
2022-07-15 18:15:00 【qq_43479892】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
小爬日常利用VBA完成SAP GUI自动化时,经常被这个问题困扰:我们进入一个事务代码界面时,如FBL1N(供应商行项目显示),很多的 GuiTextField(文本框)对象、GuiCheckBox(复选框)对象都已经有值了。我们如果忘了对这些对象的值初始化,可能SAP执行后的结果并非我们预期的那样,如下图所示。

针对这个问题,大部分童鞋很容易想到的方案是:把每个文本框的text属性设为空字符串,把每个复选框的勾选状态都设置为【不勾选】,具体代码如下:
session.findById("wnd[0]/usr/ctxtRACCT-LOW").text = "" '文本框初始化
session.findById("wnd[0]/usr/chkX\_SHBV").selected = false 'checkBox值初始化
该方法未尝不可,但是需要在SAP脚本录制阶段,对每个这类元素都初始化并记录后台脚本,属实麻烦,也容易产生遗漏。
还有一个方法是,关闭SAP连接,要求用户重新登录后,再使用我们的脚本工具,此时SAP已经对会话界面的参数完成了初始化。可该方法如果站在用户角度来思考,难免让人觉得有些非人性化,就跟解决不了别人电脑问题,就让别人重启电脑试下的感觉,小爬总觉得它不应该成为我们的最优解。
小爬翻阅了N多资料,苦心人天不负,总算总结出了更通用的方法:对会话元素层层往下穷举子元素,如果发现子元素为文本框,就将text属性置为"",如果发现子元素为复选项,就将其selected属性改为false即可。具体用VBA代码实现的话,还需要知道如何写递归函数,示例如下:
Sub SessionElementInitialization()
'对界面元素的值进行初始化,文本框值置为空,复选框状态改为【未勾选】
Dim UserArea As Object
If Not IsObject(SAPApplication) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set SAPApplication = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(Connection) Then
Set Connection = SAPApplication.Children(0)
End If
If Not IsObject(session) Then
Set session = Connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject SAPApplication, "on"
End If
Set UserArea = session.findByID("wnd[0]/usr")
Clear\_Fields UserArea
MsgBox "已完成!"
End Sub
'\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
Sub Clear\_Fields(Area As Object)
Set SAPApplication = GetObject("SAPGUI").GetScriptingEngine
Dim Obj As Object
Dim NextArea As Object
On Error Resume Next
For i = 0 To Area.Children.Count - 1
Set Obj = Area.Children(CInt(i))
If Obj.ContainerType = True Then
If Obj.Children.Count > 0 Then
Set NextArea = SAPApplication.findByID(Obj.ID)
Clear\_Fields NextArea
End If
End If
If Obj.Type Like "*TextField*" And Obj.changeable = True Then
Obj.Text = ""
ElseIf Obj.Type = "GuiCheckBox" Then
Obj.Selected = False
End If
Next i
End Sub
相信你们已经被这个问题困扰很久了,赶紧用上面的方法试下吧。对了,如果你是python爱好者,如何把上面这段VBA脚本转成python版本,相信你已经有自己的答案了(_)。
欢迎扫码关注我的公众号 获取更多爬虫、数据分析的知识!
边栏推荐
- Software architecture and design (V) -- data centric architecture
- ncnn 推理框架安装;onnx转ncnn
- Create and generate WiFi QR code mobile phone scanning link
- Qt(四)使用代码与UI文件混合开发
- Data transmission: Practice of batch extraction of isomorphic and heterogeneous IP data sources
- 达芬奇pro的FPGA学习笔记6.2--vcs和verdi开发蜂鸟e203
- [深入研究4G/5G/6G专题-37]: URLLC-8-《3GPP URLLC相关协议、规范、技术原理深度解读》-2-网络架构、时延分析与URLLC载波部署形态
- MYSQL和 ORACLE 的常见区别(一)
- PostgreSQL自带的单机并行查询的执行方法是什么?
- Lifecycle: the foundation of lifecycle aware components - jetpack series (1)
猜你喜欢

Software architecture and design (IV) -- data flow architecture

Log in and operate the database with the command line

Today, another chip IPO came out of Shenzhen Huaqiangbei

Meituan side: @transactional principle and common pits?

i.MX6ULL驱动开发 | 30 - 使用EC20 4G网卡(移植移远GobiNet驱动)

《MySQL高级篇》三、存储引擎

Selenium使用之解决‘chromedriver‘ executable needs to be in PATH.报错信息

Trial version of several common methods of database table query in SAP ABAP system

Tens of billions of data were compressed to 600gb, and tdengine was launched on GCL energy mobile energy platform

C# Channel 简单实现消息队列的发布、订阅
随机推荐
video继续播放时跳到指定位置
当使用single-branch clone仓库后,如何添加跟踪的远程分支?
Consumer start flash back
@Repository @ [email protected] Understanding of annotations
一文搞懂Go整合captcha实现验证码功能
Software architecture and design (VI) -- hierarchy
家电市场转型阵痛下,应用SaaS系统平台为家用电器行业发展注入新动能
Parent, child, and shared cursors
电容的基础知识
Create and generate WiFi QR code mobile phone scanning link
但斌投资峰会:论资产管理的重要性!
JVM memory scaling
全面解读数据中台,让企业实现数字化转型
5 practices of ITSM to ensure the agility of IT service desk
第三讲:翻转单词顺序
Windows10重置MySQL用户密码
Basic knowledge of capacitance
ncnn 推理框架安装;onnx转ncnn
MYSQL和 ORACLE 的常见区别(二)
Developing those things: how to solve the problem of long-time encoding and decoding of RK chip video processing?