Categorias

Exemplo de programa VB chamando função RFC em ABAP

Informação fornecida por Walmir Basevic.

Exemplo de programa VB chamando função RFC em ABAP

Sub Main()
    Dim LinhaComando
    Dim NomeSistema As String
    Dim Servidor As String
    Dim NumSistema As String
    Dim Usuario As String
    Dim Senha As String
    Dim Mandante As String
    Dim Idioma As String
    Dim Programa As String
    Dim Origem As String
    Dim Origem_Aux As String
    Dim Log As String
    Dim Log_Aux As String
    Dim Retorno As String
    Dim imp_numero_msg As String
    Dim imp_texto_msg As String
    Dim imp_flag_conex_ok As String
    Dim imp_flag_func_ok As String
    
    Rem *** Obtém parâmetros da linha de comando ***
    LinhaComando = GetCommandLine(11)
    
    Rem *** Move parâmetros da linha de comando para as variáveis
    NomeSistema = LinhaComando(1)
    Servidor = LinhaComando(2)
    NumSistema = LinhaComando(3)
    Usuario = LinhaComando(4)
    Senha = LinhaComando(5)
    Mandante = LinhaComando(6)
    Idioma = LinhaComando(7)
    Programa = LinhaComando(8)
    Origem_Aux = LinhaComando(9)
    Log_Aux = LinhaComando(10)
    Retorno = LinhaComando(11)
    Origem = "\" & Servidor & "sapinterfaces" & Origem_Aux
    Log = "\" & Servidor & "sapinterfaceslog" & Log_Aux
    
    Rem *** Executa a chamada da função de execução de interface SAP ***
    Call Exec_Interface(NomeSistema, _ Servidor, _ NumSistema, _ Usuario, _ Senha, _ Mandante, _ Idioma, _ Programa, _ Origem, _ Log, _ Retorno)
End Sub

Function Exec_Interface(imp_nomesyst As String, _ imp_servidor As String, _ imp_numerosyst As String, _ imp_usuario As String, _ imp_senha As String, _ imp_mandante As String, _ imp_idioma As String, _ imp_programa As String, _ imp_origem As String, _ imp_log As String, _ imp_dir_txt As String)
    Rem ***Declaração de variáveis***
    Dim g_oConnection As Object
    Dim oLogonCtrl As Object
    Dim HWND As VariantDim oFunction As Object
    Dim i_programa As ObjectDim result As Boolean
    Dim exception As ObjectDim oFunc As Object
    Dim exp_flag_conex_ok As String
    Dim exp_flag_func_ok As String
    Dim exp_numero_msg As String
    Dim exp_texto_msg As String
    
    On Error GoTo Trata_Erro
    
    exp_flag_func_ok = " "
    exp_flag_conex_ok = " "
    
    Rem ***Criar objeto de conexão com SAP***
    Set oLogonCtrl = CreateObject("SAP.Logoncontrol.1")
    Set g_oConnection = oLogonCtrl.NewConnection
    
    Rem ***Propriedades da conexão para se logar no SAP***
    g_oConnection.System = imp_nomesyst 'Nome do Sistema R/3
    g_oConnection.ApplicationServer = imp_servidor 'Nome do servidor R/3
    g_oConnection.SystemNumber = imp_numerosyst 'Número do sistema R/3
    g_oConnection.User = imp_usuario 'Usuário
    g_oConnection.Password = imp_senha 'Senha
    g_oConnection.Client = imp_mandante 'Mandante
    g_oConnection.Language = imp_idioma 'Idioma
    
    Rem ***Logar no SAP em background***
    If g_oConnection.Logon(HWND, True) = True Then
        Rem ***Mover X para flag de indicação de conexão (sucesso)***
        exp_flag_conex_ok = "X"
        
        Rem ***Criar Objeto de funções***
        Set oFunction = CreateObject("SAP.Functions")
        Set oFunction.Connection = g_oConnection
        
        Rem ***Atribuir nome da função SAP***
        Set oFunc = oFunction.Add("Z_EXEC_INTERFACES")
        
        Rem ***Atribuir parâmetros de exportação para a função SAP***
        oFunc.Exports("I_PROGRAMA") = imp_programa
        oFunc.Exports("I_ORIGEM") = imp_origem
        oFunc.Exports("I_LOG") = imp_log
        
        Rem ***Chamar a função SAP***
        If oFunc.Call Then
            Rem ***Mover X para flag de indicação de chamada de função (sucesso)***
            exp_flag_func_ok = "X"
            
            Rem ***Obter os parâmetros de retorno da função***
            exp_numero_msg = oFunc.Imports("E_NUM_MSG")
            exp_texto_msg = oFunc.Imports("E_TXT_MSG")
        End If
    End If
    
    Rem ***Gerar arquivo texto com os parâmetros de retorno***
    Open imp_dir_txt For Output As #1
    Print #1, exp_flag_conex_ok; exp_flag_func_ok; _ exp_numero_msg; exp_texto_msg
    Close #1
    Exit Function

Trata_Erro:
    exp_numero_msg = "111"
    exp_numero_msg = Err.Description
    Open imp_dir_txt For Output As #1
    Print #1, exp_flag_conex_ok; exp_flag_func_ok; _ exp_numero_msg; exp_texto_msg
    Close #1
End Function

Function GetCommandLine(Optional MaxArgs)
    ' Declara as variáveis.
    Dim C, CmdLine, CmdLnLen, InArg, I, NumArgs
    
    'Verifica se MaxArgs foi fornecido.
    If IsMissing(MaxArgs) Then
    MaxArgs = 10
    
    'Cria matriz do tamanho correto.
    ReDim ArgArray(MaxArgs)
    NumArgs = 0: InArg = False
    
    'Obtém argumentos de linha de comando.
    CmdLine = Command()
    CmdLnLen = Len(CmdLine)
    
    'Percorre um caractere da linha de comando, de cada vez.
    For I = 1 To CmdLnLen
        C = Mid(CmdLine, I, 1)
        'Verifica a presença de espaço ou tab.
        If (C <> " " And C <> vbTab) Then
            'Nenhum espaço ou tab.
            'Verifica se já está no argumento.
            If Not InArg Then
                'Novo argumento iniciado.
                'Verifica o excesso de argumentos.
                If NumArgs = MaxArgs Then Exit
                    NumArgs = NumArgs + 1
                    InArg = True
            End If
                'Concatena caractere com argumento atual.
                ArgArray(NumArgs) = ArgArray(NumArgs) & C
        Else
            'Encontrado um espaço ou tab.
            'Configura o sinalizador InArg como False.
            InArg = False
        End If
    Next I
    
    'Redimensiona a matriz de modo a conter os argumentos.
    ReDim Preserve ArgArray(NumArgs)
    
    'Retorna a matriz no nome da função.
    GetCommandLine = ArgArray()
End Function