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
|
#Region "Ouverture et fermeture du classeur"
'Déclaration-----------------------------------
Private appExcel As Excel.Application
Private Classeur As Excel.Workbook
Private Fl1, Fl2, Fl3 As Excel.Worksheet
Private myIDExcel As Int16
----------------------------------------------------
Public Sub OuvrirClasseur() 'Ouverture du classeur
'
Dim intFirstExcelIDs() As Int16 = ListID()
Try
appExcel = CType(CreateObject("Excel.Application"), Excel.Application)
'Ouverture d'un fichier Excel
Classeur = appExcel.Workbooks.Open(Filename:="C:\Users\Philippe\FCphilguio.xls", UpdateLinks:=0, ReadOnly:=False, Format:=5, Password:="Mot de passe")
Fl1 = CType(Classeur.Worksheets("Feuil1"), Excel.Worksheet)
Fl2 = CType(Classeur.Worksheets("Feuil2"), Excel.Worksheet)
Fl3 = CType(Classeur.Worksheets("Feuil3"), Excel.Worksheet)
Dim intLastExcelIDs() As Int16 = ListID()
'Différence des 2 listes et récupération de notre ID
myIDExcel = ExtractID(intFirstExcelIDs, intLastExcelIDs)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Public Function ListID() As Int16()
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 CShort(Processes.GetUpperBound(0))
intProcesses(i) = CShort(Processes(i).Id.ToString)
Next
Return intProcesses
End Function
Public 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 CShort(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
Public Sub FermerClasseur(ByVal intIDExcel As Int16) 'fermeture du fichier ouvert
' appExcel.SaveWorkspace()
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
#End Region |
Partager