Sauvegarde WorkBook Excel : mémoire protégée
Bonjour à tous.
J'ai un petit souci lors de la sauvegarde d'un fichier Excel créé via mon programme VB.NET.
Lors de la commande WorkBook.SaveAs(FileName), j'obtiens le message d'erreur
Citation:
Tentative de lecture ou d'écriture de mémoire partagée. Cela indique souvent qu'une autre mémoire est endommagée.
Malgré de longues recherches, je ne vois pas d'où peut venir le problème.
Un peu d'aide serait le bienvenu.
Voici mon code :
Code:
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| Dim AppXL As Excel.Application = Nothing
Dim WorkBook As Excel.Workbook
Dim WorkSheet As Excel.Worksheet
Dim ReportDay As String = Date.Today.AddDays(-1).ToString("dd-MM-yyyy")
Dim XLLine As Integer
Try
If _reportDatas.Rows.Count > 0 Then
FileName = Application.StartupPath & "\Alertes " & ReportDay & ".xls"
AppXL = New Excel.Application
AppXL.SheetsInNewWorkbook = 1
AppXL.Visible = True
AppXL.DisplayAlerts = False
WorkBook = AppXL.Workbooks.Add()
WorkSheet = WorkBook.ActiveSheet
With WorkSheet
.Name = "Alertes"
.Range("A1:G1").Font.Bold = True
.Range("A1:G1").Interior.ColorIndex = 15
.Cells(1, 1).Value = "V"
.Cells(1, 2).Value = "Description"
.Cells(1, 3).Value = "Lieu"
.Cells(1, 4).Value = "Secteur"
.Cells(1, 5).Value = "Start"
.Cells(1, 6).Value = "Stop"
.Cells(1, 7).Value = "Durée"
XLLine = 2
Dim DeltaT As New TimeSpan(0)
For Each row As DataRow In _reportDatas.Select("", "NAME ASC, BEGINTIME ASC")
.Cells(XLLine, 1).Value = row.Item("NAME").ToString
.Cells(XLLine, 2).Value = row.Item("TEXT").ToString
.Cells(XLLine, 3).Value = row.Item("LOCATION").ToString
.Cells(XLLine, 4).Value = row.Item("SECTEUR").ToString
.Cells(XLLine, 5).Value = CDate(row.Item("BEGINTIME")).ToString("dd/MM/yyyy HH:mm:ss")
.Cells(XLLine, 6).Value = CDate(row.Item("ENDTIME")).ToString("dd/MM/yyyy HH:mm:ss")
DeltaT = TimeSpan.FromSeconds(CDbl(row.Item("DURATION")))
.Cells(XLLine, 7).Value = DeltaT.Hours.ToString("00") & ":" & _
DeltaT.Minutes.ToString("00") & ":" & _
DeltaT.Seconds.ToString("00")
'Mise en forme selon durée
Select Case CDbl(row.Item("DURATION"))
Case Is >= (60 * 20) 'Plus de 20 minutes
.Range("G" & XLLine).Font.Bold = True
.Range("G" & XLLine).Font.ColorIndex = 2
.Range("G" & XLLine).Interior.ColorIndex = 3
Case Is >= (60 * 5) 'Plus de 5 minutes
.Range("G" & XLLine.ToString).Interior.ColorIndex = 44
End Select
XLLine += 1
Next
'Mise en page
.Range("A1:G" & XLLine).HorizontalAlignment = -4108
.Range("B2:C" & XLLine).HorizontalAlignment = -4131
'Statistiques
.Cells(XLLine + 2, 5).Value = "Nombre d'alarmes"
.Cells(XLLine + 2, 7).Value = _reportDatas.Rows.Count
.Cells(XLLine + 3, 5).Value = "Total des arrêts"
DeltaT = TimeSpan.FromSeconds(CDbl(_reportDatas.Compute("SUM(DURATION)", "")))
.Cells(XLLine + 3, 7).Value = DeltaT.Hours.ToString("00") & ":" & _
DeltaT.Minutes.ToString("00") & ":" & _
DeltaT.Seconds.ToString("00")
.Range("G" & XLLine + 2 & ":G" & XLLine + 3).HorizontalAlignment = -4108
.Range("A1:G" & XLLine).Select()
AppXL.Selection.AutoFilter()
.Columns.AutoFit()
'Légende
.Cells(XLLine + 2, 3).Value = "Durée >= 20 min."
.Range("C" & XLLine + 2).Font.Bold = True
.Range("C" & XLLine + 2).Font.ColorIndex = 2
.Range("C" & XLLine + 2).Interior.ColorIndex = 3
.Cells(XLLine + 3, 3).Value = "Durée >= 5 min."
.Range("C" & XLLine + 3).Interior.ColorIndex = 44
.Range("C" & XLLine + 2 & ":C" & XLLine + 3).HorizontalAlignment = -4108
End With
If System.IO.File.Exists(FileName) Then System.IO.File.Delete(FileName)
WorkBook.SaveAs(FileName)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
res = False
Finally
AppXL.Quit()
WorkSheet = Nothing
WorkBook = Nothing
AppXL = Nothing
End Try |