salve a tutti!
ho un problema: il mio programma controlla se un file excel contiene un foglio con un determinato nome e crea un file x discriminare i casi in cui il file è del formato corretto o meno...poi il file originario lo sposta in un'altra cartella...
in realtà su altri pc funziona tutto ok ma sul mio, girando in debug, sembra entrare nel catch del main ma esegue solo l'ultima istruzione del catch (qualsiasi essa sia!!)ho riavviato sia visual che il SO ma sempre stesso problema...che devo fare??qlc sà che potrebbe essere??
posto qui il codice:
Imports System.IO
Imports System.Diagnostics
Module Loader
Const TableName As String = "DEXTER_DATA"
Const BackupDir As String = "c:\dirbackup"
Sub Main(ByVal args As String())
Dim wFileRes As FileStream
Dim wSource As String = "SDS2E4 Directory Watcher"
Dim wLog As String = "SDS2E4"
Dim wFileName As String = Nothing
Dim wFileNameWithoutPath As String
Dim wIndex As Long
If (args.Length = 1) Then
wFileName = args(1)
Try
If (IsCorrectExcelFile(wFileName)) Then
wFileRes = File.Create(wFileName + "_Excel")
Else
wFileRes = File.Create(wFileName + "_NONExcel")
End If
wFileRes.Close()
'sposto il file elaborato in una directory di backup
If (Not Directory.Exists(BackupDir)) Then
Directory.CreateDirectory(BackupDir)
End If
wIndex = wFileName.LastIndexOf("\")
wFileNameWithoutPath = wFileName.Substring(wIndex)
File.Move(wFileName, BackupDir + wFileNameWithoutPath)
Catch ex As Exception
CreateEventLog(wSource, wLog)
EventLog.WriteEntry(wSource, "Errore Elaborando il File: " + ex.Message, EventLogEntryType.Error)
End Try
Else
CreateEventLog(wSource, wLog)
EventLog.WriteEntry(wSource, "Nessun File Da Elaborare", EventLogEntryType.Error)
End If
End Sub
Private Sub CreateEventLog(ByVal aiSource As String, ByVal aiLog As String)
If (Not EventLog.SourceExists(aiSource)) Then
EventLog.CreateEventSource(aiSource, aiLog)
End If
End Sub
Private Function IsCorrectExcelFile(ByVal aiFileName As String) As Boolean
Dim wExcelApplication As Object = Nothing
Dim wExcelWorkbooks As Object = Nothing
Dim wExcelWorkbook As Object = Nothing
Dim wSheets As Object = Nothing
Dim wSheet As Object = Nothing
Dim i As Long
Dim wNumPag As Long
Try
wExcelApplication = CreateObject("Excel.Application")
'nasconde messaggi di allerta di excel
wExcelApplication.DisplayAlerts = False
wExcelWorkbooks = wExcelApplication.Workbooks
wExcelWorkbook = wExcelWorkbooks.Open(aiFileName)
wSheets = wExcelWorkbook.Worksheets
'controllo se è un file excel valido
wNumPag = wSheets.count
For i = 0 To wNumPag - 1
wSheet = wSheets.Item(i + 1)
If (wSheet.Name = TableName) Then
IsCorrectExcelFile = True
Exit For
Else
IsCorrectExcelFile = False
End If
Next
Catch wEx As Exception
IsCorrectExcelFile = False
Finally
'close excel application
NAR(wSheet)
NAR(wSheets)
If (Not wExcelWorkbook Is Nothing) Then
wExcelWorkbook.Close(False)
End If
NAR(wExcelWorkbook)
NAR(wExcelWorkbooks)
If (Not wExcelApplication Is Nothing) Then
wExcelApplication.Quit()
End If
NAR(wExcelApplication)
End Try
End Function
'cancella tutte le variabili per la pulizia di excel
Private Sub NAR(ByVal o As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComO bject(o)
Catch
Finally
o = Nothing
End Try
End Sub
End Module