Bonjour,

J'ai créé une macro en VBA sous EXCEL pour uploader une table SAP.
Ma macro marche très bien si je la lance depuis mon fichier prinicipal excel directement (F5 depuis l'éditeur Visual Basic de Excel), mais ne marche pas si la macro est lancé lors de l'ouverture du fichier principal.
La macro n'arrive pas à ouvrir le nouveau fichier Excel généré par SAP. Il tourne en boucle (Loop ligne 91 à 93 de mon code macro AFKO)

Je ne comprends pas pourquoi?

Qui sait pourquoi?

Merci d'avance.
Pascal

Code à l'ouverture du ficher

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
 
Private Sub Workbook_Open()
Call AFKO
End Sub
Code macro AFKO
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
 
Sub AFKO()
'
Application.DisplayAlerts = False
'Launching SAP and logging into the main screen if the program is not already open
 
If IsProcessRunning("saplogon.exe") = False Then
Dim SapGui, Applic, connection, session, WSHShell, myError, WScript
 
Shell "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\saplogon.exe", vbNormalFocus
 
Set WSHShell = CreateObject("WScript.Shell")
 
Do Until WSHShell.AppActivate("SAP Logon ")
    Application.Wait Now + TimeValue("0:00:01")
Loop
 
Set WSHShell = Nothing
Set SapGui = GetObject("SAPGUI")
Set Applic = SapGui.GetScriptingEngine
Set connection = Applic.OpenConnection("10.01    PE1  -  S/4 System", True)
'error comes here if SAP-Logon and SAP is not logged in
Set session = connection.Children(0)
session.FindById("wnd[0]").Maximize
session.FindById("wnd[0]").SendVKey 0
End If
 
'Executing main screen, this part here is just setting variables for the  logged in interface. So once this is done you can enter the transaction.
 
'-----------new--------------------------
On Error Resume Next
'-----------new--------------------------
If Not IsObject(XXX) Then
   Set SapGui = GetObject("SAPGUI")
   Set XXX = SapGui.GetScriptingEngine
   '-----------new--------------------------
   myError = Err.Number
   '-----------new--------------------------
End If
If Not IsObject(connection) Then
   Set connection = XXX.Children(0)
   '-----------new--------------------------
   myError = Err.Number
   '-----------new--------------------------
End If
 
If Not IsObject(session) Then
'error comes here if SAP-Logon is open and SAP is connected
   Set session = connection.Children(0)
   '-----------new--------------------------
   myError = Err.Number
   '-----------new--------------------------
End If
'-----------new--------------------------
On Error GoTo 0
If myError <> 0 Then
  Set connection = XXX.OpenConnection("10.01    PE1  -  S/4 System", True)
'error comes here if only SAP-Logon is open and SAP not connected
  Set session = connection.Children(0)
  session.FindById("wnd[0]").Maximize
End If
'-----------new--------------------------
 
If IsObject(WScript) Then
   WScript.ConnectObject session, "on"
   WScript.ConnectObject Applic, "on"
End If
 
'Transaction comes here
 
connection.Children(0).FindById("wnd[0]").ResizeWorkingPane 352, 55, False
connection.Children(0).FindById("wnd[0]/tbar[0]/okcd").Text = "/nsqvi"
connection.Children(0).FindById("wnd[0]").SendVKey 0
connection.Children(0).FindById("wnd[0]/usr/ctxtRS38R-QNUM").Text = "AFKO"
connection.Children(0).FindById("wnd[0]/usr/btnP1").Press
connection.Children(0).FindById("wnd[0]/tbar[1]/btn[8]").Press
connection.Children(0).FindById("wnd[1]/usr/txtRS38R-DBACC").Text = "100"
connection.Children(0).FindById("wnd[1]/usr/txtRS38R-DBACC").CaretPosition = 6
connection.Children(0).FindById("wnd[1]/tbar[0]/btn[0]").Press
connection.Children(0).FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").PressToolbarContextButton "&MB_EXPORT"
connection.Children(0).FindById("wnd[0]/usr/cntlCONTAINER/shellcont/shell").SelectContextMenuItem "&XXL"
connection.Children(0).FindById("wnd[1]/tbar[0]/btn[0]").Press
connection.Children(0).FindById("wnd[1]/usr/ctxtDY_PATH").Text = "C:\TEMP\"
connection.Children(0).FindById("wnd[1]/usr/ctxtDY_FILENAME").Text = "AFKO.XLSX"
connection.Children(0).FindById("wnd[1]/usr/ctxtDY_FILENAME").CaretPosition = 4
connection.Children(0).FindById("wnd[1]/tbar[0]/btn[11]").Press
connection.Children(0).FindById("wnd[0]/tbar[0]/btn[15]").Press
connection.Children(0).FindById("wnd[0]/tbar[0]/btn[15]").Press
connection.Children(0).FindById("wnd[0]/tbar[0]/btn[15]").Press
'
Do
    DoEvents
Loop Until FichierEstOuvert("C:\TEMP\AFKO.XLSX")
Application.ScreenUpdating = True
Workbooks("AFKO.XLSX").Activate
Application.ScreenUpdating = False
Workbooks("AFKO.XLSX").Save
Workbooks("AFKO.XLSX").Close
Workbooks("MaJ Tables SAP.XLSM").Activate
'
Application.DisplayAlerts = True
End Sub
Codes des fonctions
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
Function IsProcessRunning(process As String)
Dim objList As Object
 
Set objList = GetObject("winmgmts:").ExecQuery("select * from win32_process where name='" & process & "'")
 
IsProcessRunning = objList.Count > 0
 
End Function
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
Function FichierEstOuvert(ByRef FichierTeste As String) As Boolean
 
    Dim Fichier As Long
    On Error GoTo Erreur
    Fichier = FreeFile
    Open FichierTeste For Input Lock Read As #Fichier
    Close #Fichier
    FichierEstOuvert = False
    Exit Function
Erreur:
    FichierEstOuvert = True
End Function