Link to home
Start Free TrialLog in
Avatar of Todd MacPherson
Todd MacPhersonFlag for Canada

asked on

Strange Behavior exporting table results to Excel. Excel stopped working Message (but it doesn't)

I have created a routine, based on help from here, to export table results to Excel. The code seems to work fine. It sends the correct results to excel 100% of the time but sometimes (and it is completely random) A quick pop-up occurs saying "Microsoft Office Excel has stopped working. Windows is checking for a solution to the problem"

It happens so fast that I had to to do a screen capture to see what the message said. Now here is what I do not understand...the results are correct. My code opens a pipe to excel sends the results and then closes it. There must be something missing in the code.

Please review the code and tell me where I went wrong.

Thanks

PBLack
Dim QryName As String

Dim xlApp As Object, xlWb As Object
If Me.frmViewGutsChild!PayeeID.ColumnHidden = False Then
    QryName = "tblTC_Summary_details"
Else
    QryName = "tblTC_Summary_no_details"
End If
DoCmd.OutputTo acOutputTable, QryName, acFormatXLS, strDT
    
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Open(strDT)
With xlWb
    .Worksheets(QryName).Name = "TC_Summary"
    .Save
    .Close
End With
Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing

Open in new window

Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Is that the entire code?

I don't see where you are declaring "strDT"
So we can be sure what is happening until we know if strDT is valid, or is open, ...etc

Avatar of Todd MacPherson

ASKER

oops me bad.... sorry
Dim strDT As String

If Me.frmViewGutsChild!PayeeID.ColumnHidden = False Then
    strDT = "C:\Export\TC_Summary_Detailed_" & FormatDateTime(Date, vbLongDate) & ".xls"
    With CurrentDb
        .Execute "DELETE * FROM tblTC_Summary_details"
        If Me.frmViewGutsChild.Form.FilterOn = True Then
            .Execute "INSERT INTO tblTC_Summary_details SELECT * FROM tblTCSummary WHERE " & Me.frmViewGutsChild.Form.Filter
        Else
            .Execute "INSERT INTO tblTC_Summary_details SELECT * FROM tblTCSummary"
        End If
    End With
Else
    strDT = "C:\Export\TC_Summary_" & FormatDateTime(Date, vbLongDate) & ".xls"
    With CurrentDb
        .Execute "DELETE * FROM tblTC_Summary_no_details"
        If Me.frmViewGutsChild.Form.FilterOn = True Then
            .Execute "INSERT INTO tblTC_Summary_no_details SELECT tc_pk, SlipPK, OfficeID, MillID, Mill, TC, LoadDate, LoadTime, UnloadDate, UnloadTime, TurnTime, Revenue, Cost, HSTin, HSTout, Profit, ProductID, Units, Gross, Tare, Net, MillPrice, FillionFuelAdjUnit, FillionFuelAdjTotal, PayeeFuelAdjUnit, PayeeFuelAdjTotal, PlowAdjUnit, PlowAdjTotal, BuyerHST, Paid, HSTPaid, TotPaid, HSTrateBUYER, Block, Section, Prescription, Code, Destination, ProductPK, Buyer, SaleDate, EntryDate, User FROM tblTCSummary WHERE " & Me.frmViewGutsChild.Form.Filter
        Else
            .Execute "INSERT INTO tblTC_Summary_no_details SELECT tc_pk, SlipPK, OfficeID, MillID, Mill, TC, LoadDate, LoadTime, UnloadDate, UnloadTime, TurnTime, Revenue, Cost, HSTin, HSTout, Profit, ProductID, Units, Gross, Tare, Net, MillPrice, FillionFuelAdjUnit, FillionFuelAdjTotal, PayeeFuelAdjUnit, PayeeFuelAdjTotal, PlowAdjUnit, PlowAdjTotal, BuyerHST, Paid, HSTPaid, TotPaid, HSTrateBUYER, Block, Section, Prescription, Code, Destination, ProductPK, Buyer, SaleDate, EntryDate, User FROM tblTCSummary"
        End If
    End With
End If

Dim QryName As String

Dim xlApp As Object, xlWb As Object
If Me.frmViewGutsChild!PayeeID.ColumnHidden = False Then
    QryName = "tblTC_Summary_details"
Else
    QryName = "tblTC_Summary_no_details"
End If
DoCmd.OutputTo acOutputTable, QryName, acFormatXLS, strDT
    
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Open(strDT)
With xlWb
    .Worksheets(QryName).Name = "TC_Summary"
    .Save
    .Close
End With
Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Eric Flamm
Eric Flamm
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have Option Explicit turned on and I am running AVG Antivirus. This is a 64-Bit Windows 7 box and the client is going to be using a WinXP 32 bit box. Maybe the message won't appear there or maybe something new will pop up.

Am I stuck with having to live with it?
Avatar of Norie
Norie

Try making the instance of Excel visible, then you can see if there's anything there that's causing the code to hang.
@imnorie

How do I do that?
I took a look at some of the Microsoft forums - it seems that Excel 2010 64-bit has a variety of problems which may cause the "Excel has stopped working" error, although many times the crash is more permanent than intermittent. There does not seem to be an accepted cause (maybe Macro Security, Office Trust Center, Print Drivers, and so on) or a known remedy. That said, it's possible the 32-bit version running on XP will never experience the problem.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I tried that and it gives runtime error 438
Object does not support this property or method
Sorry, my mistake.

It should be this.
xlApp.Visible = True

Open in new window

Try putting the xlApp.Visible line after the xlapp.workbooks.open line - I don't think the object (xlapp) is actually created in memory until you open a workbook in it.
eflamm

I don't think matters, it's the application that you want to be visible not the workbook.

PS You can have an instance of Excel without an open workbook.
Hi guys I changed the code a bit to keep Excel open, make it visible and keep the worksheet open. The error has stopped. At the end of the day the user is going to want to see the spreadsheet anyway so all is good. Points split for driving you nuts.

Thanks

PBLack