Search code examples
vb.netsap-gui

Trouble converting VBA SAPGUI code to VB.NET can't find proper imports libraries


I'm trying to convert code I wrote in VBA (from Access 2013) to a new VB.NET platform. I'm assuming I need to import a SAPGUI reference but can't find where to find it! I went into the package manager and installed the ... Install-Package SAP.GUI.Scripting.net -Version 1.0.0

And still can't find what I need to do next!

Here is the code I need to convert!

==============================================

Dim stat1, stat2, TEDATstr As String
Dim statl, stst2 As Integer
Session_number_max = 5  'the maximum number of possible SAP sessions
Dim session_number_(5)
On Error GoTo Erreur:
Set SapGuiAuto = GetObject("SAPGUI")

Set SAP = SapGuiAuto.GetScriptingEngine
Set Connection = SAP.Children(0)
Set session = Connection.Children(0)

'-- new session connect -----------------------------------------
session_number_all = Connection.Children.Count - 1

If Connection.Children.Count = 0 Then
 MsgBox ("Costs =") & session_number_all
 'GoTo Erreur
 End If
For i = 0 To Session_number_max - 1
     session_number_(i) = 0
Next

For session_number = 0 To session_number_all
     Set session = Connection.Children(Int(session_number))
     session_number_(session.Info.SessionNumber) =     session.Info.SessionNumber
Next

If session_number_all < Session_number_max - 1 Then
     session.CreateSession
     Do
      'WScript.sleep 500
      If Connection.Children.Count - session_number_all >= 2 Then Exit Do
     Loop
     On Error Resume Next
     Error_number = 1
     For session_number = 0 To session_number_all + 1
          Err.Clear
          Set session = Connection.Children(Int(session_number + 1))
          If Err.Number > 0 Or Err.Number < 0 Then Exit For
                   If session_number_(session.Info.SessionNumber) = 0 Then
            Error_number = 0
            Exit For
         End If

     Next
     On Error GoTo 0
Else
 MsgBox "New session not possible!"
End If



session.FindById("wnd[0]").resizeWorkingPane 150, 31, False
session.FindById("wnd[0]/tbar[0]/okcd").Text = "cn53n"
session.FindById("wnd[0]").SendVKey 0
On Error Resume Next
session.FindById ("wnd[1]/usr/ctxtTCNT-PROF_DB")
If (Err.Number = 0) Then
session.FindById("wnd[1]/usr/ctxtTCNT-PROF_DB").Text = "000000000001"
session.FindById("wnd[1]/usr/ctxtTCNT-PROF_DB").CaretPosition = 12
session.FindById("wnd[1]").SendVKey 0
End If
session.FindById("wnd[0]/usr/ctxtCN_PROJN-LOW").Text = Left(Me.envelope, 9)
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").Text =     Trim(CStr(Me.reseau))
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").SetFocus
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").CaretPosition = 7
session.FindById("wnd[0]/usr/ctxtP_DISVAR").Text = "/MJ CN53N"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/tbar[1]/btn[8]").press


 session.FindById("wnd[0]/usr/cntlALVCONTAINER/shellcont/shell").CurrentCellCo    lumn = "LST_ACTDT"

Set GridView =     session.FindById("wnd[0]/usr/cntlALVCONTAINER/shellcont/shell")

For i = 0 To GridView.RowCount - 1
If GridView.GetCellValue(i, "MLSTN") = "NRTB" Then


Me.datenrtb = GridView.GetCellValue(i, "TEDAT")
End If
If GridView.GetCellValue(i, "MLSTN") = "FCOM" Then
Me.datefcom = GridView.GetCellValue(i, "TEDAT")
End If
If GridView.GetCellValue(i, "MLSTN") = "NISR" Then
Me.datencom = GridView.GetCellValue(i, "TEDAT")
End If


Next i
session.FindById("wnd[0]/tbar[1]/btn[8]").press
session.FindById("wnd[0]/tbar[0]/btn[15]").press

session.FindById("wnd[0]").resizeWorkingPane 150, 31, False
session.FindById("wnd[0]/tbar[0]/okcd").Text = "cn46n"
session.FindById("wnd[0]").SendVKey 0
session.FindById("wnd[0]/usr/ctxtCN_PROJN-LOW").Text = Left(Me.envelope, 9)
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").Text = CStr(Me.reseau)
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").SetFocus
session.FindById("wnd[0]/usr/ctxtCN_NETNR-LOW").CaretPosition = 7
session.FindById("wnd[0]/tbar[1]/btn[8]").press 

i = 0

 Set GridView =     session.FindById("wnd[0]/usr/cntlALVCONTAINER/shellcont/shell")
stat2 = Len(GridView.GetCellValue(0, "STATXT"))
Me.etat_courrant = Mid(GridView.GetCellValue(0, "STATXT"), stat1 + 3, stat2 - stat1)
Me.etat_en_date_de = Now()
session.FindById("wnd[0]/tbar[0]/btn[15]").press
session.FindById("wnd[0]/tbar[0]/btn[15]").press
    SapGuiAuto = Nothing
    SAP = Nothing
    session = Nothing
    Exit Sub

            TempVars!enum = 5
    Exit Sub
    SapGuiAuto = Nothing
    SAP = Nothing
    session = Nothing

==============================================

Thanks

Pete


Solution

  • I had to install the .net connector from SAP, which unfortunately requires a S-USER ID. Finally managed to trace who to contact in my company to order one and download the .NET connector! Works fine now! Was just missing the connector!

    https://support.sap.com/en/product/connectors/msnet.html

    Pete