Havard Kleven
Havard Kleven

Reputation: 421

SAP Unicode Error calling Function Module in VBA

In VBA, I have created the function READ_TEXT to call the function module with the same name in SAP. To test the function, the sub tester_READ_TEXT is called with manual, hardcoded input.

Sub 'tester_READ_TEXT'

Sub tester_READ_TEXT()
    'Logon to SAP
    Dim sapConn As SAPFunctions
    Set sapConn = CreateSapFunctions(ThisWorkbook.Worksheets("Settings").Cells(2, 3).Value)
    If sapConn Is Nothing Then
        Exit Sub
    End If
    
    Call READ_TEXT(sapConn, "1", "0001", "EN", "THISISJUSTATESTOBJ   100010", "MVKE")
    

End Sub

Function 'READ_TEXT'

Public Function READ_TEXT(sapConn As SAPFunctions, CLIENT As String, ID As String, LANGUAGE_KEY As String, TDNAME As String, OBJECT As String) As Dictionary

    'LOOKS LIKE THE FM IS NOT REMOTE ACCESS ENABLED.

    Dim func As SAPFunctionsOCX.Function
    Set func = sapConn.Add("READ_TEXT")
    
    'INPUT VARIABLES (EXPORTS)
    Set expClient = func.Exports("CLIENT")
    Set expID = func.Exports("ID")
    Set expLang = func.Exports("LANGUAGE")
    Set expName = func.Exports("NAME")
    Set expObj = func.Exports("OBJECT")
    Set expArch = func.Exports("ARCHIVE_HANDLE")

    expClient.Value = CLIENT
    expID.Value = ID
    expLang.Value = LANGUAGE_KEY
    expName.Value = TDNAME
    expObj.Value = OBJECT
    expArch.Value = 0
    
    'OUTPUT VARIABLES
    Dim LINES As SAPTableFactoryCtrl.Table
    Set LINES = func.Tables("LINES")
    
    'IF CALL FAILES, EXIT
    If func.Call = False Then
        MsgBox func.Exception
    End If

    Dim salestext As CL_SalesText
    Dim dict As Dictionary
    Set dict = New Dictionary

    For i = 1 To LINES.RowCount
        Set salestext = New CL_SalesText
        
        salestext.TD = LINES.Value(i, 1)
        salestext.TDLINE = LINES.Value(i, 2)
        
        dict.Add TDNAME, salestext
        
    Next i
    
    Set READ_TEXT = dict

    Set func = Nothing
    sapConn.RemoveAll

    
End Function

The SAP logon function are created as seen below:

Public Function CreateSapFunctions(ByVal systemID As String) As SAPFunctions
    
    Dim sapConn As New SAPLogonControl
    Dim funcs As New SAPFunctions
    
    If systemID = "NO3" Then
        Set funcs.CONNECTION = CreateNO3(sapConn)
    End If
    
    If funcs.CONNECTION.Logon(0, False) <> True Then
        MsgBox "SAP Connection Failed"
    End If

    Set CreateSapFunctions = funcs

End Function


Private Function CreateNO3(ByRef sapConn As SAPLogonControl) As SAPLogonCtrl.CONNECTION

    Dim conn As SAPLogonCtrl.CONNECTION
    Set conn = sapConn.NewConnection
    
    With conn
        .ApplicationServer = "cencored"
        .system = "00"
        .user = Environ("Username")
        .systemID = "cencored"
        .CLIENT = "400"
        .LANGUAGE = "EN"
        .SNC = True
        .SNCName = "cencored"
    End With
 
    Set CreateNO3 = conn

End Function

When calling the sub, I'm getting the error

Run-time error '1001': SAP Remote Function Call Unicode

Of course, my initial thought was that it has to be some error with Unicode characters being used as parameters in the FM. Therefore, I tried to use

StrConv("THISISJUSTATESTOBJ   100010", vbUnicode)

instead of

"THISISJUSTATESTOBJ   100010"

Unfortunately, it did not solve the issue. Running the sub successfully logs onto SAP; the error is probably cased with either the input parameters being passed to the FM.

Any pointers as to what causes this error would be highly appreciated! Attached you my find the referenced applied to the workbook/project:

enter image description here

Upvotes: 0

Views: 349

Answers (0)

Related Questions