I have a question, been racking my brain trying to figure out why this is happening.

Let me start off with a chunk of code. This takes values from a textbox and writes them to an Excel sheet:

 Public Sub printFrmDUIReportPreview()

        '// =============================== PRINT PREVIEW DUI REPORT FORM ==================================

        '// Put up a quick message box telling people to be patient while this prints.
        MsgBox("Please be patient while we build your DUI report and send it to the printer. It may take up to 45 seconds. ", MsgBoxStyle.Information, "Your DUI Report Is Being Built")

        '// Start a new workbook from a template in Excel.
        Try
            Dim oExcel As Object = CreateObject("Excel.Application")
            If File.Exists(x.myDUI_ReportXlsxFile) Then '//----- check if file exists.
                '//------ Open the log template
                Dim oBook As Object = oExcel.Workbooks.Open(x.myDUI_ReportXlsxFile)
                Application.DoEvents() '// GIVE IT A SECOND TO GET ITSELF TOGETHER
                '// Add data to cells of the first worksheet in the daily log template.
                '//-- Top row with name, supervisor, etc..
                Dim oSheet As Object = oBook.Worksheets(1)
                With (oSheet) '// less code.
                    oSheet = oBook.Worksheets(1)

                    '//============= PAGE 1 OF 7 =======================

                    .Range("A1").Value = frmDUI_Interview.txtAgency.Text
                    .Range("N3").Value = frmDUI_Interview.txtDUICitationNumber.Text
                    .Range("C20").Value = frmDUI_Interview.txtAssistOfficer1.Text
                    .Range("J20").Value = frmDUI_Interview.txtAssistOfficer2.Text

                     '// PRINT
                    Application.DoEvents() '// GIVE IT A SECOND TO GET ITSELF TOGETHER
                    oExcel.Visible = True '//Open Excel
                    .PrintPreview() '//Excel Print Preview

                    ' This will and/or should kill Excel from the Task Manager Processes- Excel.exe
                    ' add all running processes to an array.
                    Dim myTaskbarProcesses() As Process = Process.GetProcesses()
                    ' loop thru the process array and locate 'excel' to terminate it.
                    For Each myProcess As Process In myTaskbarProcesses
                        If myProcess.ProcessName.ToLower = "excel" Then ' added as precaution since it might show up capitalized or not in other operating systems.
                            myProcess.Kill()
                        End If
                    Next
                End With

                '// GARBAGE COLLECTOR
                GC.Collect()
                GC.WaitForPendingFinalizers()

            Else
                MsgBox("DUI Report Printing Error. Contact DailyLog.net and let them know", MsgBoxStyle.Critical, "Print Error")
            End If
        Catch ex As Exception
            'MsgBox("Error saving as Excel", MsgBoxStyle.Critical, "Save Error")
        End Try

The problem is this: Some users on WinXP are telling me some text fields are not transferring to the spreadsheet. I don't seem to have this problem on Win7.

Is there any logical reason why this could or would happen? There are about a hundred different fields. There are a few that are hit and miss according to the users. Yet I can never duplicate it on my end, even when I use WinXP or Win7. I have also tried the last two newest versions of Office. I just can never duplicate it.

Anyone have anything off the top of their head that might be able to send me in the right direction?

Thanks in advance...

Recommended Answers

All 3 Replies

Oh- I logged in under my Facebook account. I usually ask questions as MrBungle.

You only need Application.DoEvents() if you are in a lengthy loop. You aren't doing any loops here so don't use it.

oSheet = oBook.Worksheets(1)

You don't need this inside the With at line 19. It's already been set outside on line 17.

If you are setting the values of single cells then use the Cells(row,col) property instead of Range. Row and col are integers and are one-relative.

Don't use myProcess.Kill to terminate Excel. This will kill all running copies and will severely piss off users who happen to have Excel already open (especially if they have unsaved data). Once your Excel processing is done, close the workbook and then quit the Excel application.

Try these suggestions and see if you still have a problem.

Awesome. Thanks for the tips. I'm going to do what you suggested and see if this problem is still an issue. I'll have to wait and see- the end user is on vacation this week. I'll also see if I can get the error on my end. For now I'll mark this as solved unless the error is still happening. If it is, I'll re-open the thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.