Excel crashing during macro (but very odd)

B

Brett

Hello there, I have an automatic printing macro that can be set to print to a
normal printer or print to PDF as required. I have included the relevant part
of the macro below (with comments).

In PDF mode everything functions normally (as long as I have the various
DoEvents/Wait/Sleep lines in). However, running it to go to a normal printer
causes Excel to crash when it tries to close the ActiveWorkbook (marked
below).

This problem is not new to me - I have been putting up with it for a year or
more, hence having added the WAIT etc. This has overcome it in the past, but
now it can't be solved.

It is worth noting that without the WAIT lines, in PDF mode the print
dialogue box will show that it is printing page one and then everything
freezes (including the print dialogue box). This particular problem is
currently overcome. THE PROBLEM IS IN NORMAL PRINTING MODE.

Anyone with a magic wand is most welcome to respond!
Regards, Brett

CODE AS FOLLOWS:

Workbooks.Open FileName:=pPDC, UpdateLinks:=3: Application.ScreenUpdating =
False
Application.Calculation = -4135
If EM = True Then Q50_PDF_MULTISHEET 'creates a PDF
If EM = False Then 'prints normally
Sheets("PDC 1").Visible = True: Sheets("PDC 1").Select
cnt = 1: cnt2 = 52
Do While cnt <= 15 ' prints all the valid combinations
If Cells(15, cnt2) = cnt Then
Range("BI31") = Cells(15, cnt2)
calculate
Sleep 2000: DoEvents 'otherwise Excel crashes
If TR = False Then: ActiveSheet.PrintOut From:=2, To:=2
If TR = False Then: ActiveSheet.PrintOut From:=1, To:=1
End If
cnt = cnt + 1: cnt2 = cnt2 + 2
Loop
End If
DoEvents: Application.Wait Now + TimeValue("0:0:03"): DoEvents 'otherwise
Excel crashes
ActiveWorkbook.Close savechanges:=False 'in normal print mode, Excel still
crashes on this line, but executes properly in PDF
mode''''''''''''''''''''''''''''''''
FINbk.Activate 'if the previous line is taken out then Excel will
crash on this
line (normal print only)
Range("PDC.Printed") = "Y"
printed_pdc_ok:
 
O

OssieMac

Hi Brett,

A couple of things to try. I think that the problem could be occurring due
to not all data sent to printer before code wants to close the workbook.

Go into control panel -> printers and right click the printer then
properties. Find where spooling is and and ensure spooling is selected (not
print directly to printer).

Also try turning on screen updating again before closing the workbook.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top