1/5 (2 avis)
Snippet vu 18 203 fois - Téléchargée 22 fois
Imports Microsoft.Office.Interop ' Lancer Excel, ouvrir un fichier, fermer Excel : Public Class Test Dim oExcelApp As New MSExcelApp ' Start Excel application (invisible session) and open Excel file ' Ouvre Excel en session invisible, et ouvre un fichier Excel With oExcelApp .Start(False) .OpenFile("c:\fichier_excel.xls") ' Faites ce que vous voulez ici ' Quitte Excel .Quit() End With End Class Public Class MSExcelApp Public Shared oExcelApp As Excel.Application Private intID As Int16 Public Sub Start(ByVal blnVisible As Boolean) ' Start Excel, the application can be visible or not (blnVisible) ' Lance Excel, l'application peut être visible ou non (blnVisible) ' List all the running Excel processes ' Liste tous les procesus Excel en cours Dim intFirstIDs() As Int16 = MSExcelApp.ListID() ' Open Excel ' Ouvre Excel oExcelApp = New Excel.Application oExcelApp.Visible = blnVisible ' List one more time all the running Excel processes, so we can compare with the first list ' Liste de nouveau tous les procesus Excel en cours, pour pouvoir comparer avec la première liste Dim intLastIDs() As Int16 = MSExcelApp.ListID() ' Get the ID ' Extraction de l'ID grâce à la comparaison des 2 listes intID = MSExcelApp.ExtractID(intFirstIDs, intLastIDs) End Sub ' Ouvre un fichier Excel Public Sub OpenFile(ByVal strAbsolutePath As String) oExcelApp.Workbooks.Open(strAbsolutePath) End Sub ' Ferme Excel en tuant le procesus, nous devons lui fournir en paramètre l'ID récupéré ' lors de la création de la session Excel Public Sub Quit() Try Process.GetProcessById(intID).Kill() Catch ex As Exception MessageBox.Show(ex.Message & ex.StackTrace, "Error while closing Excel.", _ MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Sub ' Fonction qui liste les procesus Private Shared Function ListID() As Int16() ' Get the ID's processes list in a array and sort it ' It is the only way to close Excel: ' To close Excel, we need to list the Excel ID's processes before user starts excel ' then we list them one more time just after opening Excel. ' We compare both of the lists we have to extract the new Excel ID of the brand new fucking Excel ' session. So we can kill this process with the ID we got, without killing other Excel user ' session. ' L'explication ci-dessus résume l'explication générale de la méthode, cf présentation du code ' Get ID's processes list Dim Processes As Process() = Nothing Processes = Process.GetProcessesByName("EXCEL") ' Load ID Processes in Array Dim intProcesses(Processes.GetUpperBound(0)) As Int16 Dim i As Int16 For i = 0 To Processes.GetUpperBound(0) intProcesses(i) = CInt(Processes(i).Id.ToString) Next Return intProcesses End Function ' Fonction qui compare les 2 listes de IDs, et nous renvoie l'ID voulu. Private Shared Function ExtractID(ByVal intFirstIDs As Int16(), ByVal intLastIDs As Int16()) As Int16 Dim intID As Int16 = Nothing Dim intID_FirsList As Int16 = Nothing Dim intID_LastList As Int16 = Nothing Dim i As Int16 = Nothing For i = 0 To intLastIDs.GetUpperBound(0) intID_LastList = intLastIDs(i) If Array.IndexOf(intFirstIDs, intID_LastList) = -1 Then intID = intID_LastList Exit For End If Next Return intID End Function End Class
23 août 2007 à 17:12
18 août 2007 à 10:30
XLDOTNET : QUITTER EXCEL SANS LAISSER D'INSTANCE EN RAM
www.vbfrance.com/code.aspx?ID=27541
La dernière version gère aussi Word :
http://patrice.dargenton.free.fr/CodesSources/VBXL.vbproj.html#7
Voir VBXL : www.vbfrance.com/code.aspx?ID=17783
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.