VB.NET -Excel Créer / Sauvegarder / Fermer

Assembly à utiliser :

Imports Microsoft.Office.Interop.Excel

Code :

Dim excel As Application = Nothing
Dim mFichier As String = “”
Dim wb As Microsoft.Office.Interop.Excel.Workbook

Private Sub objExcelcreate()
Try
mFichier =”monfichier.xlt”
excel = New Application
wb = excel.Workbooks.Open(mFichier )
excel.Visible = True
excel.DisplayAlerts = False

‘ EVENEMENT POUR DECTECTER LA FERMETURE ET  FERMER EXCEL
Try
AddHandler excel.WorkbookBeforeClose, AddressOf myExcel_Quit
Catch ex As Exception
End Try

Catch ex As Exception
MessageBox.Show(ex.Message.ToString)
End Try
End Sub

‘ FERMER EXCEL

Private Delegate Sub delegExcel()

Public Sub myExcel_Quit()
If Me.InvokeRequired Then
Me.Invoke(New delegExcel(AddressOf excel_Close))
End If
End Sub

‘ FERMER EXCEL
Public Sub excel_Close()

‘SAUVEGARDER LE FICHIER

wb.SaveAs(monfichier.xlt)
excel.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(wb)
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel)
GC.Collect()
End Sub

 

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s