Skip to Content
Aug 27, 2018 at 11:32 AM

Run and utilize SAP from Excel (vba)

564 Views Last edit Aug 28, 2018 at 08:12 AM 2 rev


I'm currently using a code to start an SAP transaction from Excel and fill the required field with variables.

This code works well but I would like to go further, by automating the "clicking button" (F8) action. (please see the attached picture and the existing code below).

Could anyone help on this and tell me if and how I can ask excel to click on the requested button (or press the F8 shortcut on my keyboard)

Thanks a lot,

VBA code :

Sub CreateFileZPPARTSap()
Dim Transaction As String
Dim Champs1 As String
Dim Champs2 As String
Dim Champs3 As String
Dim Champs4 As String
Dim Num_art As String
Dim Shortcut As String
Dim Utilisateur As String

Utilisateur = Application.UserName
Transaction = "ZMM_QD_REPORT "
Champs1 = "S_BUKRS-LOW=0101;" 'ajouter ;
Champs2 = "S_WERKS-LOW=tst1" & _
Champs3 = ";S_DATE-LOW=" & Format(Sheets("Test").Range("a1"), "") & _
            ";S_DATE-HIGH=" & Format(Sheets("Test").Range("a2"), "")
Num_art = ActiveCell

    FileNumber = FreeFile    ' Lit le numéro de fichier inutilisé
    Shortcut = "C:\Users\myID\OneDrive - myOrg\z - myOrg Settings\Desktop\Perso\sap Analyse Qualité.sap"
'// Vérifie que le fichier existe pour pouvoir lancer le reste de la macro
Txt = Dir(Shortcut)
If Txt = "" Then
   'MsgBox "Le fichier n'existe pas", vbInformation, "ATTENTION :"
    'MsgBox "Le fichier " & Txt & " existe"
    'supprime le fichier exporté ==> Shortcut
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(Shortcut) '(où Shorcut est le path complet + nom du fichier)
End If
    Open Shortcut For Append As #FileNumber
    Print #FileNumber, "[System]"
    Print #FileNumber, "Name=sap"
    Print #FileNumber, "Description=sap(sapnbr)"
    Print #FileNumber, "Client=000"
    Print #FileNumber, "[User]"
    Print #FileNumber, "Name=id"   'A CHANGER
    Print #FileNumber, "Language=FR"
    Print #FileNumber, "[Function]"
    Print #FileNumber, "COMMAND=" & Transaction & Champs1 & Champs2 & Num_art & Champs3
    Print #FileNumber, "[Configuration]"
    Print #FileNumber, "WorkDir=C:\Users\" & Utilisateur & "\Documents\SAP"
    Print #FileNumber, "[Options]"
    Print #FileNumber, "Reuse=1"
    Close #FileNumber
    Shell ("C:\Program Files (x86)\SAP\FrontEnd\SapGui\sapshcut.exe " & Shortcut)

End Sub


sap.png (86.1 kB)