- Public Class Form1
-
- 'Ajouter la référence au projet : Microsoft Excel Object Library
- 'Déclaration de notre objet Excel.
-
- Dim xlApp As Excel.Application
-
- Private 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 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
-
- Private 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
-
- Private Sub closeExcelFile(ByVal intIDExcel As int16)
-
- If intIDExcel <> 0 Then
- If Process.GetProcessById(intIDExcel).HasExited = False Then
- Try
- Process.GetProcessById(intIDExcel).Kill()
- intIDExcel = 0
- Catch ex As Exception
- MessageBox.Show(ex.Message & ex.StackTrace, "Error while closing Excel integration.", _
- MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End If
- End If
- End Sub
-
- Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
-
- dim myIDExcel As int16
-
- 'L'idée ici est de récupérer la liste d'ID des process qui tournent sur le PC. On recupère la
- 'liste avant de faire un New et on recupère la liste après avoir fait le New. Ainci il ne reste
- 'plus qu'à faire la différence des 2 listes grâce à la fonction ExtractID(,) pour retrouver le
- 'bon ID de notre process Excel. Il ne reste plus qu'à la killer une fois notre application
- 'terminée.
-
- 'Première liste
- Dim intFirstExcelIDs() As Int16 = ListID()
-
- 'Création de notre objet et attribution de l'ID
- xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
-
- 'Seconde liste
- Dim intLastExcelIDs() As Int16 = ListID()
-
- 'Différence des 2 listes et récupération de notre ID
- myIDExcel = ExtractID(intFirstExcelIDs, intLastExcelIDs)
-
- 'Ici vous faites ce que vous voulez avec votre application Excel
- '
- '
- '
- 'Une fois terminée, vous pouvez fermer votre processus
-
- closeExcelFile(myIDExcel)
-
- End Sub
-
- End Class
Public Class Form1
'Ajouter la référence au projet : Microsoft Excel Object Library
'Déclaration de notre objet Excel.
Dim xlApp As Excel.Application
Private 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 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
Private 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
Private Sub closeExcelFile(ByVal intIDExcel As int16)
If intIDExcel <> 0 Then
If Process.GetProcessById(intIDExcel).HasExited = False Then
Try
Process.GetProcessById(intIDExcel).Kill()
intIDExcel = 0
Catch ex As Exception
MessageBox.Show(ex.Message & ex.StackTrace, "Error while closing Excel integration.", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dim myIDExcel As int16
'L'idée ici est de récupérer la liste d'ID des process qui tournent sur le PC. On recupère la
'liste avant de faire un New et on recupère la liste après avoir fait le New. Ainci il ne reste
'plus qu'à faire la différence des 2 listes grâce à la fonction ExtractID(,) pour retrouver le
'bon ID de notre process Excel. Il ne reste plus qu'à la killer une fois notre application
'terminée.
'Première liste
Dim intFirstExcelIDs() As Int16 = ListID()
'Création de notre objet et attribution de l'ID
xlApp = CType(CreateObject("Excel.Application"), Excel.Application)
'Seconde liste
Dim intLastExcelIDs() As Int16 = ListID()
'Différence des 2 listes et récupération de notre ID
myIDExcel = ExtractID(intFirstExcelIDs, intLastExcelIDs)
'Ici vous faites ce que vous voulez avec votre application Excel
'
'
'
'Une fois terminée, vous pouvez fermer votre processus
closeExcelFile(myIDExcel)
End Sub
End Class