Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

nom des fichiers et des applications

1 réponse
Avatar
berkowil
Bonjour tout le monde

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

Merci de votre aide

1 réponse

Avatar
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)

Private Const GW_HWNDNEXT& = 2
Private Const WM_CLOSE = &H10

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