-1

I am looking to automate a simple fetch from SAP GUI for Windows to Microsoft Excel. SAP GUI has a neat macro recorder and I've noticed that when assigning the file path, it gives to option to record or playback either .vbs or .js files. When I attempt to save the record as .js, the output is still in VBA. I don't know VBA and Microsoft's documentation for it stinks (or I might be spoiled by both the MDN Web Docs and Ruby API Docs).

The simple solution here is to suck it up and learn VBA but inquiring minds want to know! Is it possible to actually save those macros in JavaScript? If not, is there a way to use JavaScript in Excel without an add-in? The add-ins all require node.js as a prerequisite and Admin rights are locked air tight on my company's work PCs.

I did figure out a way to hook to the Excel JavaScript API by using var Excel = new ActiveXObject("Excel.Application"); but it's a lot more difficult to convince non tech-savvy people to double click a .js file than it is to have them click a button within a .xlsx file.

OPB
  • 11
  • 2

1 Answers1

0

There is probably a way to do it, but I think it will be cumbersome to get to the final result.

You can easily create a macro and assign it to a button in the Excel with the following code:

Sub SAPFullProcess()
'******** VBA REFERENCES ********
'Reference to sapfewse.ocx (if not available - Browse... and search for the file C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.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

This piece of code will connect to the currently open SAP Session and will perform the operations in there. You just need to add the steps from the recorder in this line ' ** Here will continue all the steps from the recorder **

Only thing needed for this to run correctly is to go to VBA Editor -> Tools -> References... and look for SAP GUI Scripting API. If it is not there, you can click on Browse... and search for this ocx file: C:\Program Files\SAP\FrontEnd\SAPgui\sapfewse.ocx

AlexRivax
  • 214
  • 1
  • 3