Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Currently working on a project where I automatically track individual users system lock/unlock time and write that information to an excel sheet placed on network drive (using INTEROP). This exe is placed on the system startup so that when the user logins it automatically starts. The idea behind this application is to track user locked/unlocked time to calculate their utilization without user knowledge (Personally I would hate this kind of hidden approach, but this is what I’m expected to come up with)

The exe works absolutely fine till the time the user is on the office network, but the problem is if the user logs in from home then he will be out of the network till the time he connects to the VPN. But if the user locks/unlocks the system before he gets on the VPN, the tool throws an error "Microsoft Excel cannot open the file, check spelling or path".

I don’t want this to happen because the tool is throwing the path along with the error and the whole purpose of having this application hidden goes for a toss. Just wanted to check with you guys if there is anyway I can suppress this error message and close this application if this happens.

Any help on this will be very useful. I have given the code below


VB
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles    MyBase.Load

    Me.Visible = False
    Me.ShowInTaskbar = False


    Timer1.Start()
    Timer1.Interval = 100
    RichTextBox1.Text = ""
End Sub


Private Sub WorkStationReader_locked(ByVal ivarreturn As Object) Handles WorkStationReader.locked
    If ivarreturn(1) = True And Locked = False Then
        Locked = True
        ''MsgBox(ivarreturn(0).ToString)
        '  MsgBox(ivarreturn(0).ToString & "" & Date.Now.ToString)

        RichTextBox1.Text += ivarreturn(0).ToString & "" & Date.Now.ToString
        RichTextBox1.Text += vbCrLf

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        xlApp = New Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        xlApp.Visible = False

        Try

            Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open("\\99.99.999.99\Excel\Satish.xlsx")
            Dim xlSheet As Excel.Worksheet = xlBook.ActiveSheet
            Dim xlSheet2 As Excel.Worksheet = xlBook.Worksheets("Sheet1")
            ' xlSheet2.Range("B5").Value2 = "my new string"
            Dim iRow As Integer
            iRow = 1
            With xlApp
                Do While .Cells(iRow, 1).value <> ""
                    .Cells(iRow, 1).activate()
                    iRow = iRow + 1
                Loop

                .Cells(iRow, 1).value = ivarreturn(0).ToString & "" & Date.Now.ToString
            End With
            xlBook.Save()
            xlApp.Quit()


            Dim strX As String = RichTextBox1.SelectedText
            'if no text is selected, copy the entire text
            If strX.Length = 0 Then strX = RichTextBox1.Text
            'Copy data if any to copy
            If strX.Length > 0 Then
                Clipboard.SetDataObject(strX)
            End If
        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End If





    If ivarreturn(1) = False And Locked = True Then
        Locked = False
        RichTextBox1.Text += ivarreturn(0).ToString & "" & Date.Now.ToString
        RichTextBox1.Text += vbCrLf
        ''MsgBox(ivarreturn(0).ToString)

        Dim xlApp As Microsoft.Office.Interop.Excel.Application
        xlApp = New Microsoft.Office.Interop.Excel.Application
        Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook
        Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
        xlApp.Visible = False




        Dim xlBook As Excel.Workbook = xlApp.Workbooks.Open("\\99.99.999.99\Excel\Satish.xlsx")


        Dim xlSheet As Excel.Worksheet = xlBook.ActiveSheet
        Dim xlSheet2 As Excel.Worksheet = xlBook.Worksheets("Sheet1")
        ' xlSheet2.Range("B5").Value2 = "my new string"
        Dim iRow As Integer

        iRow = 1
        With xlApp
            Do While .Cells(iRow, 2).value <> ""
                .Cells(iRow, 2).activate()
                iRow = iRow + 1
            Loop

            .Cells(iRow, 2).value = ivarreturn(0).ToString & "" & Date.Now.ToString
        End With
        xlBook.Save()
        xlApp.Quit()

    End If

End Sub

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    WorkStationReader.WorkStationISLocked()
End Sub
Posted
Updated 10-Sep-14 18:06pm
v2
Comments
Sergey Alexandrovich Kryukov 11-Sep-14 0:22am    
Don't you think that the root of your problem is that you are obliged to do the work you hate? In such cases, it's too hard to help. But I do appreciate that you realized that.
—SA
Satish Narayanan 11-Sep-14 0:34am    
Thank you. Just that I need to come up with this whether I like it nor not.
Richard MacCutchan 11-Sep-14 3:54am    
You should check the path manually before trying to open it in Excel. If you get a failure then stop the application.
Sinisa Hajnal 11-Sep-14 8:55am    
Or do the unthinkable: have empty catch block around application object :o Just know that this is bad, bad thing to do :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900