cancel
Showing results for 
Search instead for 
Did you mean: 

Run and utilize SAP from Excel (vba)

Former Member
0 Kudos

Hello,

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" & _
          ";S_WERKS-HIGH=tst2"
Champs3 = ";S_DATE-LOW=" & Format(Sheets("Test").Range("a1"), "dd.mm.yyyy") & _
            ";S_DATE-HIGH=" & Format(Sheets("Test").Range("a2"), "dd.mm.yyyy")
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 :"
Else
    '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)
    f.Delete
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

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member186338
Active Contributor
0 Kudos

You are violating forum rules:

Unreal name: D4 L6

Not related tag: EPM Add-In for Excel

https://www.sap.com/community/about/rules-of-engagement.html