SAP GUI脚本引擎是否可以用Javascript而不是Visual Basic输出宏



我希望自动化从SAP GUI for Windows到Microsoft Excel的简单提取。SAP GUI有一个整洁的宏记录器,我注意到在分配文件路径时,它提供了录制或播放.vbs或.js文件的选项。当我尝试将记录保存为.js时,输出仍在VBA中。我不知道VBA和微软的文档,因为它很臭(或者我可能被MDN Web文档和Ruby API文档宠坏了(。

这里的简单解决方案是学习VBA,但好奇的人想知道!真的可以用JavaScript保存这些宏吗?如果没有,有没有一种方法可以在没有插件的情况下在Excel中使用JavaScript?插件都需要node.js作为先决条件,并且管理员权限在我公司的工作电脑上被气密锁定

我确实找到了一种使用var Excel = new ActiveXObject("Excel.Application");连接ExcelJavaScriptneneneba API的方法,但要说服非技术熟练的人双击.js文件比让他们单击.xlsx文件中的按钮要困难得多。

可能有一种方法可以做到这一点,但我认为要得到最终结果会很麻烦。

您可以使用以下代码轻松创建宏并将其分配给Excel中的按钮:

Sub SAPFullProcess()
'******** VBA REFERENCES ********
'Reference to sapfewse.ocx (if not available - Browse... and search for the file C:Program FilesSAPFrontEndSAPguisapfewse.ocx)
'******** VBA REFERENCES ********

'*****************************
'ATTACH SAP Window
'*****************************
Dim SapGuiAuto As Object
Dim Application As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim Session As SAPFEWSELib.GuiSession

Set SapGuiAuto = GetObject("SAPGUI")
If Not IsObject(SapGuiAuto) Then
Exit Sub
End If

Set Application = SapGuiAuto.GetScriptingEngine()
If Not IsObject(Application) Then
Exit Sub
End If

Set Connection = Application.Children(0)
If Not IsObject(Connection) Then
Exit Sub
End If

Set Session = Connection.Children(0)
If Not IsObject(Session) Then
Exit Sub
End If

'Exit in case SAP is not open
If err.Number <> 0 Then
MsgBox "SAP is not open, please open a session and try again."
Set Session = Nothing
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing
Exit Sub
End If

On Error GoTo ErrHandler
Session.findById("wnd[0]").maximize
Session.findById("wnd[0]/tbar[0]/okcd").text = "MIRO"
Session.findById("wnd[0]").sendVKey 0
' ** Here will continue all the steps from the recorder **

'Reset Variables
Set Session = Nothing
Set Connection = Nothing
Set Application = Nothing
Set SapGuiAuto = Nothing

Exit Sub

ErrHandler:
MsgBox err.Description
End Sub

这段代码将连接到当前打开的SAP会话,并在其中执行操作。您只需要在' ** Here will continue all the steps from the recorder **中添加来自记录器的步骤

要使其正确运行,只需要转到VBA编辑器->工具->引用并查找SAP GUI Scripting API。如果没有,您可以单击浏览并搜索此ocx文件:C:\Program Files\SAP\FrontEnd\SAPgui \sapfewse.ocx

最新更新