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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| Global ExcelApp() As String
Global Index As Long
'Tu doit entrer le nom de la fenetre a fermer exactement comme il est ecrit
'dans cette fenetre ou dans la fenetre qui s'ouvre avec "Ctrl - Alt - Suppr"
'FindWindow Recherche le Handle de la fenetre spacifiée dans lpWindowName
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
'PostMessage Envoi l'ordre de fermeture à la fenêtre specifiée par FindWindow
' Déclaration des API utilisées
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, _
ByVal lParam As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
' Déclaration de type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
'Déclaration de constante
Const WM_CLOSE = &H10
' Variable temporaire utilisée uniquement par la fonction GetWindowsList pour stocker les fenêtres.
Public stGetWindowsList As String
' Nombre de popup supprimées début le lancement
Public lgKill As Long
' Redimensionnement automatique des colonnes du contrôle ListView
Public Const LVM_FIRST = &H1000
Public Const LVM_SETCOLUMNWIDTH = LVM_FIRST + 30
Public Const LVSCW_AUTOSIZE = -1
Public Function FermerFenetre(stTitreFenetre As String) As Boolean
' Cette fonction ferme la fenêtre dont le titre exact est passé en paramètre.
Dim REP As Long
' Recherche du "handle" de la fenêtre
REP = FindWindow(vbNullString, stTitreFenetre)
If REP Then
' Envoi l'ordre de fermeture à la fenêtre
REP = PostMessage(REP, WM_CLOSE, vbNull, vbNull)
End If
End Function
Sub FermerExcelCommeUneBrute()
'fermer excel Brutalement
GetWindowsList
FermerExcel
End Sub
Sub FermerExcel()
On Error GoTo Err_FermerExcel
Dim Titre
For Each Titre In ExcelApp
FermerFenetre (Titre)
Next
Exit Sub
Err_FermerExcel:
End Sub
Sub TestExcel()
On Error GoTo GetWindowsList
Dim Titre
For Each Titre In ExcelApp
MsgBox "Le programme ne fonctionne que si toutes les fenêtre Excel Sont fermées" & Chr(13) & Chr(10) _
& "Veuillez enregistrer les fichiers excel avant de cliquer sur OK", vbCritical, "Enregistrer application Excel"
FermerFenetre (Titre)
Next
Exit Sub
GetWindowsList:
End Sub
Public Sub GetWindowsList() 'As Boolean
' Cette fonction retourne sous forme de tableau l'ensemble
' des titres des fenêtres de premier niveau ouvertes sous windows.
Dim lgRep As Long
stGetWindowsList = vbNullString
' Appel de l'API et envoi du pointeur vers notre fonction de rappel
lgRep = EnumWindows(AddressOf EnumWindowsProc, 0)
End Sub
' Déclaration de la fonction de rappel
Public Function EnumWindowsProc(ByVal lgHwnd As Long, ByVal lgParam As Long) As Long
Dim stTmp As String, lgTmp As Long, lgRet As Long
Dim stTmp2 As String, tmpRect As RECT
'stTmp = Space$(201)
stTmp = String(200, Chr$(0))
lgTmp = 200
' On récupère le titre de la fenêtre à partir du handle
lgRet = GetWindowText(lgHwnd, stTmp, lgTmp)
stTmp = Left$(stTmp, InStr(stTmp, Chr$(0)) - 1)
If UCase(stTmp) Like "*MICROSOFT EXCEL*" And Len(stTmp) > Len("MICROSOFT EXCEL") Then
ReDim Preserve ExcelApp(Index)
ExcelApp(UBound(ExcelApp)) = stTmp
Index = Index + 1
End If
'MsgBox stTmp
'stTmp = Trim$(Replace(stTmp, Chr$(0), vbNullString))
If (stTmp <> vbNullString) Then
' Récupère les dimensions de la fenêtre
lgRet = GetWindowRect(lgHwnd, tmpRect)
' Ajoute les dimensions de la fenêtre à la chaîne retournée
stTmp = Format$(tmpRect.Right - tmpRect.Left, "00000000") & " " & stTmp
stTmp = Format$(tmpRect.Bottom - tmpRect.Top, "00000000") & " " & stTmp
End If
If (stTmp <> vbNullString) Then
stTmp = Format$(lgHwnd, "00000000000;-0000000000") & " " & stTmp
End If
' Stockage du résultat dans la chaine temporaire (ajout au texte existant).
' On pourrait imaginer construire une chaîne plus complexe en ajoutant également le handle
' de la fenêtre, ce qui permettrait des manipulations externes (comme une fermeture par exemple).
If (Trim$(stTmp) <> vbNullString) Then stGetWindowsList = stGetWindowsList & stTmp & vbCrLf
' Retourne 1 systématiquement
EnumWindowsProc = 1
End Function
Public Function FileToString(stFile As String) As String
' Lecture d'un fichier et récupération à l'intérieur d'une chaîne unique
' Attention, au ralentissement pour les fichiers de taille importante.
' A n'utiliser que sur les fichiers de type texte.
On Error GoTo errFileToString
Dim inFree As Integer
Dim stTmp As String
inFree = FreeFile
Open stFile For Input Shared As #inFree
stTmp = Input(LOF(inFree), #inFree)
Close #inFree
FileToString = stTmp
Exit Function
errFileToString:
FileToString = vbNullString
End Function
Public Function StringToFile(stString As String, stFile As String) As String
' Enregistrement d'une chaine dans un fichier. En cas de réussite la fonction retourne une chaîne vide
' sinon, un message d'erreur.
' Cette fonction est l'inverse de la précédente
Dim inFree As Integer
Dim stTmp As String
On Error GoTo ErrStringToFile
inFree = FreeFile
Open stFile For Output As #inFree
Print #inFree, stString
Close #inFree
StringToFile = vbNullString
Exit Function
ErrStringToFile:
StringToFile = "Erreur (" & Err.Number & "), " & Err.Description
End Function
Public Function InList(Liste) As String
'Public Function InList(lstListe As ListView, stElement As String, Optional blRespecteCasse As Boolean = True) As Long
' Vérifie la présence d'un élément dans une liste
On Error GoTo err_InList
Dim i As Variant
For i = 0 To 10000
If UCase(Liste(i)) Like "*MICROSOFT EXCEL*" Then
InList = CStr(Liste(i))
Exit Function
End If
Next i
Exit Function
err_InList:
InList = -1
End Function |
Partager