1/5 (33 avis)
Vue 25 172 fois - Téléchargée 880 fois
' clsExcelHost : Classe pour héberger Excel ' ============ ' Title: EXCEL.EXE Process Killer ' Description: After many weeks of trying to figure out why the EXCEL.EXE Process ' does not want to go away from the Task Manager, I wrote this class that will ensure ' that the correct EXCEL.EXE Process is closed. This is after using Excel.Application ' via Automation from a VB.NET/ASP.NET application. ' This file came from Planet-Source-Code.com... the home millions of lines of source code ' You can view comments on this code/and or vote on it at: ' http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1998&lngWId=10 ' The author may have retained certain copyrights to this code... ' please observe their request and the law by reviewing all copyright conditions ' at the above URL. ' Author: I.W Coetzer 2004/01/22 ' *Thanks Dan for the process idea. ' Classe commentée et légèrement modifiée par Patrice Dargenton le 05/11/2004 ' *Solution to the EXCEL.EXE Process that does not want to go away from task manager. ' ' IMPLEMENTATION (EXAMPLE OF THE CLASS IN USE) ' 'Public Sub TestXL() ' Dim oXLH As clsExcelHost ' Try ' oXLH = New clsExcelHost ' Catch ' MsgBox("Excel n'est pas installé !") ' Exit Sub ' End Try ' oXLH.xlApp.Workbooks.Add() ' oXLH.xlApp.Range("A1") = "Hello World!" ' oXLH.xlApp.Workbooks(1).SaveAs("C:\Test.xls") ' oXLH.xlApp.Workbooks(1).Close() ' oXLH.xlApp.Quit() ' oXLH.xlApp = Nothing ' oXLH.Quitter() ' = Process.GetProcessById(xl.ProcId).Kill() ' oXLH = Nothing ' MsgBox("C:\Test.xls a été créé avec succès !") ' Dim p As New Process ' p.StartInfo = New ProcessStartInfo("C:\Test.xls") ' p.Start() 'End Sub Public Class clsExcelHost Public xlApp As Object Private ProcId% Public Sub New() ProcId = 0 ' Liste des processus avant le mien Dim Process1() As Process = Process.GetProcesses() xlApp = CreateObject("Excel.Application") ' Liste des processus après le mien : la différence me donnera l'Id du mien Dim Process2() As Process = Process.GetProcesses() Dim i%, j% Dim bMonProcessXL As Boolean For j = 0 To Process2.GetUpperBound(0) If Process2(j).ProcessName = "EXCEL" Then bMonProcessXL = True ' Parcours des processus avant le mien For i = 0 To Process1.GetUpperBound(0) If Process1(i).ProcessName = "EXCEL" Then If Process2(j).Id = Process1(i).Id Then ' S'il existait avant, ce n'était pas le mien bMonProcessXL = False Exit For End If End If Next i If bMonProcessXL = True Then ' Maintenant que j'ai son Id, je pourrai le tuer ' xlApp.Hinstance ne fonctionne pas avec Excel 2000 ' alors que cette méthode marche toujours ! ProcId = Process2(j).Id Exit For End If End If Next j End Sub Public Sub Quitter() If ProcId = 0 Then Exit Sub Process.GetProcessById(ProcId).Kill() End Sub 'Protected Overrides Sub Finalize() ' MyBase.Finalize() 'End Sub End Class
22 févr. 2007 à 08:20
21 févr. 2007 à 17:07
Je ferais plus attention la prochaine fois, désolé. :0(
21 févr. 2007 à 16:52
21 févr. 2007 à 16:46
(par rapport au code de Eldim)
ce qui n'engage que moi...
21 févr. 2007 à 16:30
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.