A extrair dados da SAP utilizando Macros do Excel

Estou a tentar tirar dados da SAP usando macros do excel. Sou novo na VBA, por isso, por favor, tenha paciência comigo. Encontrei um tópico aqui chamado VBA a recolher dados de SAP para bonecos e estou confuso. O que estou a tentar fazer é o seguinte:
  1. Copie um número de notificação de uma lista no excel.
  2. Vá para o ecrã apropriado em SAP e cole este número na caixa de pesquisa.
  3. Abra a longa caixa de texto.
  4. copia o texto longo.
  5. colar no excel.

Aqui está a ligação a vba a extrair dados da SAP para bonecos

parece que não consigo chegar ao Set session = connection.Children (0) 'Get the first session (window) on that connection.

qualquer ajuda é muito apreciada. A razão pela qual estou fazendo isso é porque a SAP não vai exportar o longtext e é preciso um ato de Deus para consertá-lo.

Author: Community, 2014-06-19

2 answers

Isto é o que eu uso para todas as minhas ligações para o SAP:

'Connect to SAP to run automation.
        If Not IsObject(SAP_applic) Then
            Set SapGuiAuto = GetObject("SAPGUI")
            Set SAP_applic = SapGuiAuto.GetScriptingEngine
        End If
            Set connection = SAP_applic.Children(0)
        If Not IsObject(connection) Then
            Set connection = application.Children(0)
        End If
        If Not IsObject(session) Then
            Set session = connection.Children(0)
        End If
        If IsObject(WScript) Then
            WScript.ConnectObject session, "on"
            WScript.ConnectObject application, "on"
        End If
 1
Author: DeerSpotter, 2015-11-24 22:31:21
Se estou a ler isto correctamente, estás a querer extrair a informação do texto de uma notificação. Se esse for o caso, eu tenho um arquivo de texto que você pode importar para o editor visual basic e, em seguida, executar essa macro em sua planilha. A única coisa que você precisa ter é a primeira coluna contendo a lista de seu número de notificação em sua planilha (certifique-se de que o primeiro número de notificação começa na célula A2). Na célula A1, número de notificação de entrada ou algo assim. Para a descrição de entrada da célula B2, portanto, você saberá o que cada coluna representa. Não sei se está a usar o iqs3 da transacção para aceder à sua notificação, mas é aqui que vou ver todas as nossas notificações que foram criadas. Se não, então espero que este modelo meu ainda ser de algum uso para você como um go by ou algo assim.

Basta copiar e colar o seguinte código abaixo no bloco de notas e gravá-lo em algum lugar a que possa aceder ao importar para a sua folha de cálculo.

Dim i As Integer
Sub Main()

    Call MsgBox("Excel will minimize during this task to allow you to do some other work while it runs. " _
                & vbCrLf & "" _
                & vbCrLf & "It takes approximately 9 seconds per EWR number to retrieve the data from SAP." _
                & vbCrLf & "" _
                & vbCrLf & "Thanks for your patience and understanding, while the code runs. :)" _
                , vbInformation, "See you soon!")

    With Application
        .ScreenUpdating = False
        .Cursor = xlWait
        .Visible = False
    End With
    On Error GoTo Main_Error

    If Not IsObject(sapApplication) Then
        Set SapGuiAuto = GetObject("SAPGUI")
        Set sapApplication = SapGuiAuto.GetScriptingEngine
    End If
    If Not IsObject(Connection) Then
        Set Connection = sapApplication.Children(0)
    End If
    If Not IsObject(Session) Then
        Set Session = Connection.Children(0)
    End If
    If IsObject(WScript) Then
        WScript.ConnectObject Session, "on"
        WScript.ConnectObject sapApplication, "on"
    End If
    i = 2
    'For i = 2 To LastRow(Sheet1)
    Do Until Cells(i, 1).Value = ""
        If Cells(i, 1).Value = "" Then GoTo errReturn
        Application.StatusBar = "Row: " & i & ": Retrieving details for EWR: " & Cells(i, 1).Value
        Cells(i, 2) = Populate(Session, Cells(i, 1).Value, i)
        Cells(i, 1).VerticalAlignment = xlCenter
        Cells(i, 2).VerticalAlignment = xlCenter
        Cells(i, 2).HorizontalAlignment = xlCenter
        If Not Cells(i, 2).MergeCells = True Then Rows.AutoFit
        i = i + 1
        DoEvents
        'Next i
    Loop

    Columns("A:B").AutoFit

    On Error GoTo 0
errReturn:
    With Application
        .ScreenUpdating = True
        .Cursor = xlNormal
        .StatusBar = False
        .Visible = True
    End With
    Exit Sub

Main_Error:
    MsgBox "You need to connect to the SAP GUI to use this spreadsheet", vbCritical, "Error"
    GoTo errReturn
End Sub

Function Populate(Session, EWRNumber As String, j As Integer) As String
    On Error GoTo continue
    Dim strpopulate As String
    'Dim j As Integer
    strpopulate = ""
    'j = 1
    With Session
        '.findById("wnd[0]").maximize
        .findById("wnd[0]/tbar[0]/okcd").Text = "/nIQS3"
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/usr/ctxtRIWO00-QMNUM").Text = EWRNumber
        .findById("wnd[0]").sendVKey 0
        .findById("wnd[0]/usr/tabsTAB_GROUP_10/tabp10\TAB01/ssubSUB_GROUP_10:SAPLIQS0:7235/subCUSTOM_SCREEN:SAPLIQS0:7212/subSUBSCREEN_1:SAPLIQS0:7715/btnQMICON-LTMELD").press
        .findById("wnd[0]/mbar/menu[2]/menu[2]").Select
        n = 1
        Do Until .findById("wnd[0]/usr/tblSAPLSTXXEDITAREA/txtRSTXT-TXLINE[2," & n & "]").Text = "________________________________________________________________________"
            strpopulate = strpopulate & .findById("wnd[0]/usr/tblSAPLSTXXEDITAREA/txtRSTXT-TXLINE[2," & n & "]").Text
            strpopulate = strpopulate & vbCrLf
            n = n + 1
            'MsgBox (CDbl(n / 29) = CInt(n / 29))
            If CDbl(n / 29) = CInt(n / 29) Then
                Call MergeCells(j)    '= 29
                i = i + 1
                'j = j + 1
            End If
        Loop
        .findById("wnd[0]/tbar[0]/btn[15]").press
        .findById("wnd[0]/tbar[0]/btn[15]").press
    End With
    'MsgBox strpopulate
continue:
    Debug.Print strpopulate
    Populate = strpopulate
End Function

Sub MergeCells(j As Integer)
    Cells(j, 1).Select
    'Insert row below active cell
    ActiveCell.Offset(1).EntireRow.Insert

    'Merge Selected Cells and Newly inserted Cells
    Cells(j, 1).Select
    Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(1, 0)).Merge
    Cells(j, 2).Select
    Range(ActiveCell.Offset(0, 0), ActiveCell.Offset(1, 0)).Merge

    ActiveCell.Select
    Cells(j, 1).VerticalAlignment = xlCenter
    Cells(j, 2).VerticalAlignment = xlCenter
    Cells(j, 2).HorizontalAlignment = xlCenter
    Cells(j, 2).WrapText = True
    Rows(j).RowHeight = 409
    Rows(j + 1).RowHeight = 409
End Sub
 1
Author: Brian, 2017-11-14 18:44:26