Dim xlapp As Object xlapp = CreateObject("Excel.Application") Dim xlWorkBook As Excel.Workbook Dim xlWorkSheet As Excel.Worksheet Dim FilePath As String = "C:\1.xls" If System.IO.File.Exists(FilePath) Then xlWorkBook = xlapp.workbooks.open(FilePath, False) Else xlWorkBook = xlapp.workbooks.add() End If If String.IsNullOrEmpty(xlWorkBook.Path) Then xlWorkBook.SaveAs(FilePath, Excel.XlFileFormat.xlWorkbookNormal) End If If xlWorkBook.Sheets.Count() < 1 Then xlWorkSheet = CType(xlWorkBook.Worksheets.Add(), Excel.Worksheet) xlWorkSheet.Name = "Log Sheet" Else xlWorkSheet = xlWorkBook.Worksheets(1) End If Dim SheetRow As Integer = 0 For SheetRow = 1 To xlWorkSheet.Rows.Count If Trim(xlWorkSheet.Cells(SheetRow, 1).value) = "" Then xlWorkSheet.Name = "Log Sheet" xlApp.Cells(1, 1) = "File access path " xlApp.Cells(1, 2) = "File Name" xlApp.Cells(1, 3) = "File Access By User" xlApp.Cells(1, 4) = "Date" xlApp.Cells(1, 5) = "Time" xlWorkSheet.Columns.Range("A1:E1").Font.Bold = True xlWorkSheet.Columns.Range("A1:E1").Font.Color = Color.Blue xlWorkSheet.Columns.Range("A1:E1").Font.Size = 15 Exit For End If Next Dim iRow As Integer = 0 Dim iCol As Integer = 0 For iRow = 3 To xlWorkSheet.Rows.Count If Trim(xlWorkSheet.Cells(iRow, 1).value) = "" Then xlWorkSheet.Cells(iRow, 1).Value = sDirectory xlWorkSheet.Cells(iRow, 2).Value = sFileName Dim AccessDate As String = String.Empty Dim AccessTime As String = String.Empty AccessDate = AccessDate.Trim & DateTime.Now.Day AccessDate = AccessDate.Trim & "-" & DateTime.Now.ToString("MMMM") AccessDate = AccessDate.Trim & "-" & DateTime.Now.Year & " " AccessTime = AccessTime.Trim & DateTime.Now.Hour AccessTime = AccessTime.Trim & ":" & DateTime.Now.Minute & " " AccessTime = AccessTime.Trim & " " & DateTime.Now.ToString("tt") xlWorkSheet.Cells(iRow, 3).Value = My.Settings.timeSheetLoginUserType xlWorkSheet.Cells(iRow, 4).Value = AccessDate xlWorkSheet.Cells(iRow, 5).Value = AccessTime Exit For ' BAIL OUT IF REACHED THE LAST ROW. Else End If Next xlWorkBook.Save() xlApp.DisplayAlerts = False xlWorkBook.Close() System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) xlWorkSheet = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook) xlWorkBook = Nothing System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp) xlApp = Nothing