Bonjour
j'essaie d'exécuter une macro d'un classeur spécifique et dans une instance aussi spécifique parmi plusieurs instances de excel.
le GetObject(CheminFichier).Application semble bien cibler la bonne instance, pourtant
obj.Activate
xl.Visible = True
ne semble pas donner le focus à la fenêtre en question si au départ c'est une autre instance qui a le focus.
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
'les variables
 dim xl,wb,NomFichier,CheminFichier
 NomFichier ="Essai_macro.xlsm"
 CheminFichier ="C:/Users/toto/Desktop/Essai_macro.xlsm"
  'on vérifie si excel ouvert
 On Error Resume Next
 	Set xl = GetObject(CheminFichier).Application  
 On Error Goto 0
 'si excel ouvert
 if xl = "Microsoft Excel" then
 	set wb = Nothing
 'on vérifie si le document est ouvert
 	For Each obj In xl.Workbooks
	'MsgBox obj.Name
 		If obj.Name = NomFichier Then  'use obj.FullName for full path
 			set wb = obj
			obj.Activate
 			xl.Visible = True
 			xl.run "Macro_hello"
 			Exit For
 		End If
 	Next
End If