简体   繁体   English

使用vba的RFC多个表而不会丢失连接

[英]RFC Multiple tables with vba without losing connections

Hello i'm refactoring a RFC code of SAP Extraction, and i'm getting a problem with some functions. 您好,我正在重构SAP Extraction的RFC代码,并且遇到一些功能问题。 I'm only doing this because with have a lot of worksheets that extract a bunch of information from SAP, we need something faster and easier to understand. 我之所以这样做,是因为拥有大量可从SAP提取大量信息的工作表,我们需要更快,更容易理解的东西。

 Public ctlTableFactory, RFC_READ_TABLE, eQUERY_TAB, tblOptions, tblData, tblFields, funcControl, objConnection, ctlLogon, strExport1, strExport2
Public Sub conectasap()
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    tempo_inicio = Now()

    Set objFileSystemObject = CreateObject("Scripting.FileSystemObject")
    Set ctlLogon = CreateObject("SAP.LogonControl.1")
    Set funcControl = CreateObject("SAP.Functions")
    Set ctlTableFactory = CreateObject("SAP.TableFactory.1")
    Set objWindowsScriptShell = CreateObject("WScript.Shell")
    Set objConnection = ctlLogon.NewConnection

    objConnection.ApplicationServer = "XXXXXXXXXXXX"
    objConnection.SystemNumber = "XXXX"
    objConnection.Client = "XXX"
    objConnection.Language = "PT"
    objConnection.User = "XXXXXXXXXXXXX"
    objConnection.Password = "Cockpit1314"
    booReturn = objConnection.logon(0, True)

    If booReturn <> True Then

        MsgBox "Não foi possível conectar ao SAP. " + vbCrLf + vbCrLf + "1. Verifique sua conexão à internet" + vbCrLf + "2. Verifique a conexão do SAP" + vbCrLf + "3. Verifique se o computador possue o programa SAP" + vbCrLf + vbCrLf + "Caso persistir o problema, contacte o suporte.", vbOKOnly + vbInformation
        Stop
    End If

    funcControl.Connection = objConnection
    Set RFC_READ_TABLE = funcControl.Add("RFC_READ_TABLE")
    Set strExport1 = RFC_READ_TABLE.exports("QUERY_TABLE")
    Set strExport2 = RFC_READ_TABLE.exports("DELIMITER")
    Set tblOptions = RFC_READ_TABLE.Tables("OPTIONS")
    Set tblData = RFC_READ_TABLE.Tables("DATA")
    Set tblFields = RFC_READ_TABLE.Tables("FIELDS")

    Extrai_VBAK
    Extrai_VBAP
    'Extrai_VBEP RFC_READ_TABLE, strExport1, strExport2, tblOptions, tblData, tblFields
    'Extrai_MVKE RFC_READ_TABLE, strExport1, strExport2, tblOptions, tblData, tblFields

    objConnection = Nothing
    tempo_fim = Now() - tempo_inicio
    MsgBox tempo_fim

    'Call apply_formulas

    Application.Calculation = xlCalculationAutomatic
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

When i call the function "Extrai_VBAK" it works perfectly, but when it goes to the other functions it simply does not download anything from SAP. 当我调用函数“ Extrai_VBAK”时,它可以正常工作,但是当转到其他函数时,它根本不会从SAP下载任何内容。

FUNCTION CODE: 功能码:

    Function Extrai_VBAK()



    CREAT_DATE = Format(Now - 2, "YYYYMMDD")

    Sheets("VBAK").Select
    Range("A2:X200000").ClearContents

'//As funções abaixo são as conexões que o SAP precisa fazer para extrair as tabelas futuramente.
    strExport1.Value = "VBAK"
    strExport2.Value = ";"

'// VBELN = Numero do pedido
    tblFields.AppendRow
    tblFields(1, "FIELDNAME") = "VBELN"

'// AUART = Tipo do pedido
    tblFields.AppendRow
    tblFields(2, "FIELDNAME") = "AUART"

'// AUGRU = Motivo da ordem
    tblFields.AppendRow
    tblFields(3, "FIELDNAME") = "AUGRU"

'// KUNNR = Código do cliente
    tblFields.AppendRow
    tblFields(4, "FIELDNAME") = "KUNNR"

'// ERDAT = Data de criação
    tblFields.AppendRow
    tblFields(5, "FIELDNAME") = "ERDAT"

'// ERNAM = Nome da pessoa que criou
    tblFields.AppendRow
    tblFields(6, "FIELDNAME") = "ERNAM"

'// VDATU = Data de entrega
    tblFields.AppendRow
    tblFields(7, "FIELDNAME") = "VDATU"

'// KNUMV = Código da condição
    tblFields.AppendRow
    tblFields(8, "FIELDNAME") = "KNUMV"

'// LIFSK = Bloqueio de remessa
    tblFields.AppendRow
    tblFields(9, "FIELDNAME") = "LIFSK"

'// KVGR4 = Grupo do cliente / Distribuidor
    tblFields.AppendRow
    tblFields(10, "FIELDNAME") = "KVGR4"

'// KVGR5 = Grupo do cliente
    tblFields.AppendRow
    tblFields(11, "FIELDNAME") = "KVGR5"


'// Filtra para extrair apenas BR10
    tblOptions.AppendRow
    tblOptions(1, "TEXT") = "VKORG EQ 'BR10'"

'// Data de criação -2 dias
    tblOptions.AppendRow
    tblOptions(2, "TEXT") = "AND VDATU GE '" & CREAT_DATE & "' "

'// Elimina IC5067
    tblOptions.AppendRow
    tblOptions(3, "TEXT") = "AND KUNNR NE 'IC5067    ' "

    If RFC_READ_TABLE.call = True Then

        If tblData.RowCount > 0 Then

            For intRow = 1 To tblData.RowCount


                For coluna = 1 To 1

                    Cells(intRow + 1, coluna).Value2 = tblData(intRow, coluna)

                Next

            Next

        Else


        End If
    Else
        End

    End If



    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

    Range("A2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.TextToColumns Destination:=Range("A2"), DataType:=xlDelimited, _
    TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
    Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(Array(1, 1), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
    Range("A1").Select



    L = 2
    Do Until Cells(L, 1) = Empty
        Cells(L, 10).Value2 = Trim$(Cells(L, 10).Value2)

        L = L + 1
    Loop
    L = L - 1

End Function

Are you sure the problem is that you lose the connection between the first and the second call? 您确定问题出在第一通电话和第二通电话之间的连接丢失了吗? In that case I would expect an error message like "Connection broken", when trying to make the seconc call. 在那种情况下,尝试进行seconc调用时,我会收到诸如“连接断开”的错误消息。 But according to your descriptin there is no error message. 但是根据您的描述,没有错误消息。 Instead the second call is "successfull", but just doesn't return any data, is that right? 相反,第二个调用是“成功的”,但是不返回任何数据,对吗?

Perhaps you need to create a fresh RFC_READ_TABLE object for each call? 也许您需要为每个调用创建一个新的RFC_READ_TABLE对象?

We might also see more, if you activate RFC trace (eg environment variable RFC_TRACE=2) and take a look at the data that is sent and received during the second call. 如果您激活RFC跟踪(例如环境变量RFC_TRACE = 2)并查看第二个调用期间发送和接收的数据,我们可能还会看到更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM