Dans mon cas, je roule tout dans Excel.
Et c'est Excel qui gère les temps d'attente de chaque commande.
Le vbs ne me sert qu'à construire/modifier/adapter le code généré par l'enregistreur.
Ensuite, je me crée des variables au besoin que j'utilise dans le script.
Ces genres de lignes, je les place dans mon code Excel
De là l'utilisation de la variable Session déclarée dans Excel.
session.findById("wnd[0]/tbar[0]/okcd").text = "/nmb25"
Le code que je t'ai mis n'était pas complet, mais j'ai laissé quelques lignes à la fin pour que tu vois quelques findbyid.
Une fois le script vbs enregistré, je ne l'utilise plus jamais, donc jamais de Shell "..."
Tu peux aussi utiliser des librairies de SAP dans Excel pour aider à créer ton code.
Par la suite, tu peux enlever les références et utiliser des variables As Object pour une meilleure portabilité.
Lorsque tu déclares
Dim App, Connection, session As Object
Seule Session est un objet. Les autres sont des Variant
Habitue-toi à déclarer chaque variable comme il se doit
Dim App as Object, Connection as Object, session As Object
Voici un exemple de code que j'utilise à partir d'Excel.
(Les Stop...Resume me servent à déboguer seulement. Ça permet de trouver la ligne en problème avec F8)
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 77 78 79 80 81 82 83 84 85
| Sub ZWBBS(Chemin As String, Fichier As String, strDate As String)
Dim SapGuiAuto As Object
Dim AppliSAP As Object
Dim Connection As Object
Dim Session As Object
On Error GoTo Erreur
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
Session.findById("wnd[0]").Maximize
Session.findById("wnd[0]/tbar[0]/okcd").Text = "/nZWBBS"
Session.findById("wnd[0]").sendVKey 0
'Variante
Session.findById("wnd[0]/tbar[1]/btn[17]").press
Session.findById("wnd[1]/usr/txtV-LOW").Text = Nom de la variante
Session.findById("wnd[1]/usr/txtENAME-LOW").Text = Ton UserID
Session.findById("wnd[1]/tbar[0]/btn[8]").press
'Paramètres
Session.findById("wnd[0]/usr/ctxtP_DATE").Text = strDate 'la date au format reconnu passé en paramètre
Session.findById("wnd[0]/usr/ctxtP_VARI").Text = La variante d'affichage s'il y a lieu
Session.findById("wnd[0]/tbar[1]/btn[8]").press
'Sauvegarde
Session.findById("wnd[0]/usr/cntlCONTENEUR_ALV/shellcont/shell/shellcont[1]/shell").pressToolbarContextButton "&MB_EXPORT" 'petit bouton Excel
Session.findById("wnd[0]/usr/cntlCONTENEUR_ALV/shellcont/shell/shellcont[1]/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 du fichier passé en paramètre
Session.findById("wnd[1]/usr/ctxtDY_FILENAME").Text = Fichier 'inscrit le nom du fichier passé en paramètre
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
Set Session = Nothing
Set Connection = Nothing
Set AppliSAP = Nothing
Set SapGuiAuto = Nothing
Exit Sub
Erreur:
If Err.Number = -2147221020 Or Err.Number = 614 Then
MsgBox "Démarrer une session SAP", vbExclamation, "Erreur SAP"
ElseIf Err.Number = 619 Then
MsgBox Err.Description & vbCrLf & "S'il y a une session BW ouverte, la fermer avant de rouler la macro", vbInformation, "Erreur SAP"
ElseIf Err.Number = 617 Then
Session.findById("wnd[0]").sendVKey 0
Resume Next
ElseIf Err.Number = 613 Then
Resume Next
Else
MsgBox Err.Number & vbCrLf & Err.Description
Stop
Resume
End If
' Stop
' Resume
End Sub |
Partager