1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| Sub ZSE16N()
Dim SapGuiAuto As Object
Dim AppliSAP As SAPFEWSELib.GuiApplication
Dim Connection As SAPFEWSELib.GuiConnection
Dim Session As SAPFEWSELib.GuiSession
Dim Chemin As String, Fichier As String
Dim nbLignes As Long
Set SapGuiAuto = GetObject("SAPGUI")
If Not IsObject(SapGuiAuto) Then
Exit Sub
End If
Set AppliSAP = SapGuiAuto.GetScriptingEngine()
If Not IsObject(AppliSAP) Then
Exit Sub
End If
Set Connection = AppliSAP.Connections(0)
If Not IsObject(Connection) Then
Exit Sub
End If
Set Session = Connection.Sessions(0)
If Not IsObject(Session) Then
Exit Sub
End If
Chemin = "C:\"
Fichier = "Resultat.xls"
Session.FindById("wnd[0]").Maximize
Session.FindById("wnd[0]/tbar[0]/okcd").Text = "/nSE16N" '"/nZSE16N"
Session.FindById("wnd[0]").SendVKey 0
Session.FindById("wnd[0]/usr/ctxtGD-TAB").Text = "MARC"
Session.FindById("wnd[0]").SendVKey 0
'Sélection de codes
Session.FindById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/btnPUSH[4,1]").SetFocus
Session.FindById("wnd[0]/usr/tblSAPLSE16NSELFIELDS_TC/btnPUSH[4,1]").Press
'Ouverture du fichier qui contient les codes nécessaires
Workbooks.Open "C:\Codes.xlsx"
nbLignes = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
ActiveSheet.Range("A2:A" & nbLignes).Copy
Session.FindById("wnd[1]/tbar[0]/btn[24]").Press 'Copie les codes dans SAP
Session.FindById("wnd[1]/tbar[0]/btn[8]").Press 'Ferme la fenêtres des codes
Session.FindById("wnd[0]/tbar[1]/btn[8]").Press 'Démarre la transaction
ActiveWorkbook.Close False 'Ferme le fichier
'Sauvegarde du fichier
Session.FindById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").PressToolbarContextButton "&MB_EXPORT"
Session.FindById("wnd[0]/usr/cntlRESULT_LIST/shellcont/shell").SelectContextMenuItem "&PC"
Session.FindById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").Select
Session.FindById("wnd[1]/usr/subSUBSCREEN_STEPLOOP:SAPLSPO5:0150/sub:SAPLSPO5:0150/radSPOPLI-SELFLAG[1,0]").SetFocus
Session.FindById("wnd[1]/tbar[0]/btn[0]").Press
Session.FindById("wnd[1]/usr/ctxtDY_PATH").Text = Chemin 'Inscrit le chemin
Session.FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = Fichier 'inscrit le nom du fichier
If Dir(Chemin & Fichier) <> "" Then Kill Chemin & Fichier 'supprime le fichier s'il existe déjà
Session.FindById("wnd[1]/tbar[0]/btn[0]").Press 'sauvegarde
mgbox "Fichier sauvegardé"
Exit Sub
Erreur:
If Err.Number = -2147221020 Or Err.Number = 614 Then
MsgBox "Démarrer une session SAP", vbExclamation, "Erreur SAP"
Application.WindowState = xlMaximized
Else
MsgBox Err.Number & vbCrLf & Err.Description
End If
End Sub |
Partager