Skip to Content
Author's profile photo Former Member

VB.NET Control Over SAP Instance

I’m very new to SAP and am much more comfortable coding within VB.NET at this point. So I was happy to discover that you can control much of SAP directly from VB.NET through the SAPbouiCOM.Application object. Below is an example of this. First we create our VB.NET form:

Private oSBOApp As SAPbouiCOM.Application

‘New instance of a form – frmMain

Public Sub New(ByRef inputApp As SAPbouiCOM.Application)

  ‘ This call is required by the designer.

  InitializeComponent()

  ‘ Add any initialization after the InitializeComponent() call.

  If inputApp IsNot Nothing Then

    oSBOApp = inputApp

  Else

    MsgBox(“Could not find SBO Application to manipulate”)

    Me.Close()

    Exit Sub

  End If

End Sub

Then, we create a new instance of that form from within an SAP AddOn, passing along our SAPbouiCOM.Application (in this case, called Me.m_oSBO_Application):

Private Sub loadForm()

  Try

    ‘Make sure only one instance of this form ever exists

    Dim oOpenforms = Application.OpenForms.OfType(Of frmMain)()

    If oOpenforms.Any() Then

      Exit Sub

    End If

    ‘Instantiate the form and load it

    Dim oSBOApp = Me.m_oSBO_Application

    If oSBOApp IsNot Nothing Then

      Dim frmInstance As New frmMain(oSBOApp)

      Application.Run(frmInstance)

      Application.DoEvents()

    End If

    Catch ex As Exception

      MsgBox(ex.Message)

    End Try

End Sub

Finally, below is the code that you want to place where you want the application to be triggered/loaded from. Note that you want to start the form in its own thread so that it’s non-modal and doesn’t lock up SAP itself:

‘Open form in new thread

Dim oThread As New Threading.Thread(AddressOf loadForm)

oThread.SetApartmentState(System.Threading.ApartmentState.STA)

oThread.Start()

Once you’ve opened the VB.NET form in this way, you now have access to ALL of the revealed SBO objects by direct manipulation, such as this (assuming that the Inventory Transfer form is the active form):

‘Inventory Transfer matrix

Dim oITMatrix As SAPbouiCOM.Matrix = oSBOApp.Forms.ActiveForm.Items.Item(“23”).Specific

oITMatrix.Clear()

BAM! The Inventory Transfer form’s matrix is now an empty space.

I’m sure the old pros know all about this, but as a new guy, I thought it was pretty cool. Now if only SAP would reveal more of the SBO Application objects to us….

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.