mon programme "maitre.xls" appelle l'ouverture d'un autre fichier et d'une
autre occurence d'excel de cette facon
rep = Shell("C:\Program Files\Microsoft Office\Office10\excel.exe
D:\esclave.xls", vbMaximizedFocus)
Maintenant j'aimerai savoir si le programme a bien été fermé avant de
l'envoyer par outlook(ça je sais faire grace a votre aide!)
j'ai bien trouvé comment faire pour des classeurs appartenant a la meme
application mais pas pour des classeurs appartenant a d'autres applications
Cette action est irreversible, confirmez la suppression du commentaire ?
Signaler le commentaire
Veuillez sélectionner un problème
Nudité
Violence
Harcèlement
Fraude
Vente illégale
Discours haineux
Terrorisme
Autre
PMO
Bonjour,
Une solution avec le code suivant où il faudra adapter, à votre usage, les constantes cernées par des ###
1) la Sub OpenWorkbookInNewInstance n'est là que pour illustrer. Elle ouvre un classeur dans une nouvelle instance 2) la Sub CloseInstance fait ce que vous demandez. A savoir, fermer un classeur dans une autre instance Excel. Si ce dernier a subi des modifications un message vous en avertit.
Code à copier
***************** '### A adapter ### Const MON_FICHIER As String = "ExportImage.xls" Const CHEMIN As String = "C:" '#################
Private Declare Function GetActiveWindow& Lib "user32" () Private Declare Function GetWindow& Lib "user32" _ (ByVal hwnd As Long, ByVal wCmd As Long) Private Declare Function GetWindowText& Lib "user32" _ Alias "GetWindowTextA" (ByVal hwnd As Long, _ ByVal lpString As String, ByVal cch As Long) Private Declare Function GetWindowTextLength& Lib "user32" _ Alias "GetWindowTextLengthA" (ByVal hwnd As Long) Private Declare Function PostMessage& Lib "user32.dll" _ Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub CloseInstance() Dim bool As Boolean Dim i& Dim retour& Dim hwnd& Dim Nom$ Dim LenNom& hwnd& = GetActiveWindow Do Until hwnd& = 0 refaire: LenNom& = GetWindowTextLength(hwnd&) If LenNom& Then Nom$ = Space(LenNom&) GetWindowText hwnd&, Nom$, LenNom& + 1 If Nom$ = "Microsoft Excel - " & MON_FICHIER Then bool = True Else Nom$ = "" End If If bool Then i& = i& + 1 If i& > 1 Then MsgBox prompt:="Des changements sont intervenus dans le classeur " & MON_FICHIER & _ vbCrLf & "Il demande si vous voulez le sauvegarder.", _ Title:="Demande d'enregistrement des modifications dans " & MON_FICHIER, _ Buttons:=vbInformation Exit Sub End If retour& = PostMessage(hwnd&, WM_CLOSE, 0, ByVal 0&) Sleep (100) bool = False GoTo refaire End If i& = 0 hwnd& = GetWindow(hwnd&, GW_HWNDNEXT) Loop End Sub
Sub OpenWorkbookInNewInstance() Dim rep& rep& = Shell(Application.Path & "EXCEL.EXE" & Space(1) & _ CHEMIN & MON_FICHIER, vbMaximizedFocus) End Sub *****************
Cordialement.
PMO Patrick Morange
Bonjour,
Une solution avec le code suivant où il faudra adapter, à votre usage, les
constantes cernées par des ###
1) la Sub OpenWorkbookInNewInstance n'est là que pour illustrer. Elle ouvre
un classeur dans une nouvelle instance
2) la Sub CloseInstance fait ce que vous demandez. A savoir, fermer un
classeur dans une autre instance Excel.
Si ce dernier a subi des modifications un message vous en avertit.
Code à copier
*****************
'### A adapter ###
Const MON_FICHIER As String = "ExportImage.xls"
Const CHEMIN As String = "C:"
'#################
Private Declare Function GetActiveWindow& Lib "user32" ()
Private Declare Function GetWindow& Lib "user32" _
(ByVal hwnd As Long, ByVal wCmd As Long)
Private Declare Function GetWindowText& Lib "user32" _
Alias "GetWindowTextA" (ByVal hwnd As Long, _
ByVal lpString As String, ByVal cch As Long)
Private Declare Function GetWindowTextLength& Lib "user32" _
Alias "GetWindowTextLengthA" (ByVal hwnd As Long)
Private Declare Function PostMessage& Lib "user32.dll" _
Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, ByVal lParam As Long)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub CloseInstance()
Dim bool As Boolean
Dim i&
Dim retour&
Dim hwnd&
Dim Nom$
Dim LenNom&
hwnd& = GetActiveWindow
Do Until hwnd& = 0
refaire:
LenNom& = GetWindowTextLength(hwnd&)
If LenNom& Then
Nom$ = Space(LenNom&)
GetWindowText hwnd&, Nom$, LenNom& + 1
If Nom$ = "Microsoft Excel - " & MON_FICHIER Then bool = True
Else
Nom$ = ""
End If
If bool Then
i& = i& + 1
If i& > 1 Then
MsgBox prompt:="Des changements sont intervenus dans le classeur " &
MON_FICHIER & _
vbCrLf & "Il demande si vous voulez le sauvegarder.", _
Title:="Demande d'enregistrement des modifications dans " &
MON_FICHIER, _
Buttons:=vbInformation
Exit Sub
End If
retour& = PostMessage(hwnd&, WM_CLOSE, 0, ByVal 0&)
Sleep (100)
bool = False
GoTo refaire
End If
i& = 0
hwnd& = GetWindow(hwnd&, GW_HWNDNEXT)
Loop
End Sub
Sub OpenWorkbookInNewInstance()
Dim rep&
rep& = Shell(Application.Path & "EXCEL.EXE" & Space(1) & _
CHEMIN & MON_FICHIER, vbMaximizedFocus)
End Sub
*****************
Une solution avec le code suivant où il faudra adapter, à votre usage, les constantes cernées par des ###
1) la Sub OpenWorkbookInNewInstance n'est là que pour illustrer. Elle ouvre un classeur dans une nouvelle instance 2) la Sub CloseInstance fait ce que vous demandez. A savoir, fermer un classeur dans une autre instance Excel. Si ce dernier a subi des modifications un message vous en avertit.
Code à copier
***************** '### A adapter ### Const MON_FICHIER As String = "ExportImage.xls" Const CHEMIN As String = "C:" '#################
Private Declare Function GetActiveWindow& Lib "user32" () Private Declare Function GetWindow& Lib "user32" _ (ByVal hwnd As Long, ByVal wCmd As Long) Private Declare Function GetWindowText& Lib "user32" _ Alias "GetWindowTextA" (ByVal hwnd As Long, _ ByVal lpString As String, ByVal cch As Long) Private Declare Function GetWindowTextLength& Lib "user32" _ Alias "GetWindowTextLengthA" (ByVal hwnd As Long) Private Declare Function PostMessage& Lib "user32.dll" _ Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _ ByVal wParam As Long, ByVal lParam As Long) Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub CloseInstance() Dim bool As Boolean Dim i& Dim retour& Dim hwnd& Dim Nom$ Dim LenNom& hwnd& = GetActiveWindow Do Until hwnd& = 0 refaire: LenNom& = GetWindowTextLength(hwnd&) If LenNom& Then Nom$ = Space(LenNom&) GetWindowText hwnd&, Nom$, LenNom& + 1 If Nom$ = "Microsoft Excel - " & MON_FICHIER Then bool = True Else Nom$ = "" End If If bool Then i& = i& + 1 If i& > 1 Then MsgBox prompt:="Des changements sont intervenus dans le classeur " & MON_FICHIER & _ vbCrLf & "Il demande si vous voulez le sauvegarder.", _ Title:="Demande d'enregistrement des modifications dans " & MON_FICHIER, _ Buttons:=vbInformation Exit Sub End If retour& = PostMessage(hwnd&, WM_CLOSE, 0, ByVal 0&) Sleep (100) bool = False GoTo refaire End If i& = 0 hwnd& = GetWindow(hwnd&, GW_HWNDNEXT) Loop End Sub
Sub OpenWorkbookInNewInstance() Dim rep& rep& = Shell(Application.Path & "EXCEL.EXE" & Space(1) & _ CHEMIN & MON_FICHIER, vbMaximizedFocus) End Sub *****************