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

PBLackAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeffrey CoachmanMIS LiasonCommented:
Is that the entire code?

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

PBLackAuthor Commented:
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

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Eric FlammOwnerCommented:
Unless Option Explicit is turned on, Access won't complain about the undeclared variable strDT. It knows the 4th argument should be a string (filename), so it will dim strDT as an empty string (or an empty variant, I'm not sure which), which will cause doCmd.OutputTo to prompt for a destination file name, which I assume is what the OP is experiencing.

The fact that Excel is throwing an intermittent "not responding" is probably related to other demands on the PC - if Excel can't get enough processor cycles, Windows acts like Excel has hung - just waiting a while for Excel to catch up (as described by OP) is usually effective. I have run into this problem sometimes when a particularly nosy anti-virus product was running and interfering with Excel being able to save the file, but I suspect other utility programs might have a similar effect.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PBLackAuthor Commented:
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?
NorieAnalyst Assistant Commented:
Try making the instance of Excel visible, then you can see if there's anything there that's causing the code to hang.
PBLackAuthor Commented:
@imnorie

How do I do that?
Eric FlammOwnerCommented:
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.
NorieAnalyst Assistant Commented:
Pblack

Immediately after CreateObject make Excel visible:
Set xlApp = CreateObject("Excel.Application")

xlApp.Visible

Open in new window

Like I said this might help you find out if there's anything happening in Excel that's causing the code to hang.

There are 2 things I can think of that could cause that, the wrong filename/path in strDT or worksheet name in QryName.

There could be other things of course.
PBLackAuthor Commented:
I tried that and it gives runtime error 438
Object does not support this property or method
NorieAnalyst Assistant Commented:
Sorry, my mistake.

It should be this.
xlApp.Visible = True

Open in new window

Eric FlammOwnerCommented:
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.
NorieAnalyst Assistant Commented:
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.
PBLackAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.