VBA code to create PDFs causing Excel to crash immediately without any hang

DaveyD

New Member
Joined
May 20, 2015
Messages
31
Hi, I have a VBA procedure that loops through each file in a directory and does the following:
1. Opens the file
2. Selects 2 sheets
3. Exports those sheets to a PDF

Excel is crashing during this process at random. I.e., one time it can crash after 6 PDF files created, and another after 10 files etc.
The interesting thing is that Excel is crashing without hanging at all! The window just immediately closes and excel is closed! (Never saw this before)

Another thing to note, if I run the code in debug mode, and step through each file one at a time, then it works perfectly.
I.e., I put a break point right before the line to export to PDF. Each time it hit the break point, I just pressed F5 to continue to the next file. Excel got through all the files without crashing.
Can it be a speed issue? I.e., creating too many files to export at the same time?

If anyone has any idea what this can be, please let me know
Thanks,
David


This is the line of code that I use to export:
Code:
        ActiveSheet.ExportAsFixedFormat _
            Type:=xlTypePDF, _
            fileName:=fileName, _
            Quality:=xlQualityStandard, _
            IncludeDocProperties:=True, _
            IgnorePrintAreas:=False, _
            OpenAfterPublish:=False
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
I Have this Code

Sub TurnTabIntoPDF1()
'
' TurnTabIntoPDF Macro
'
Range("A1:T22").Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$T$22"
With ActiveSheet.PageSetup
.Orientation = xlLandscape
.PaperSize = xlPaperA5
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ActiveWorkbook.Path & "" & .Range("B6").Value, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True

End With
End Sub

It make a PDF file
 
Upvote 0
@ranman256, I began to try that but then realized that I won't be able to name the file which is very important, so I didn't continue down that path
@Hjemmet, thank you, but that seems to be the same code. I also have other code to make pdf that works fine and even more so, this code used to work fine!
But now, for some reason, it is crashing without any error messages


Any other suggestions?
 
Upvote 0
Click on "File" and then "Save as" here you can make your choise

or press"CTRL and P" then you can choise Print a PDF file under Printer choise
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top