Intermittent 400 and 1004 VBA errors with a macro to save the active worksheet as a PDF file

BlissC

New Member
Joined
Aug 28, 2017
Messages
47
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Earlier this month I was trying to figure out how to add a print button to a worksheet to print selected cells as a PDF document - https://www.mrexcel.com/forum/excel...ksheet-print-selected-cells-pdf-document.html - which as you can see from that thread, I eventually figured out using this code:


Code:
Sub Save_Timesheet_As_PDF()


With ActiveSheet.PageSetup
    .CenterHeader = "Time Allocation Sheet"
    .Orientation = xlLandscape
    .PrintArea = "$A$1:$I$40"
    .Zoom = False
    .FitToPagesTall = 1
    .FitToPagesWide = 1
End With


ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:="Time Allocation Sheet", _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=True


End Sub

This was working fine on my test spreadsheet, which I was working on at home. This morning though as I couldn't email it to myself at work as a macro enabled Excel file, I had to save the file as a .xlsx and then once I'd got it saved on the system at work, then re-assign the macros to the appropriate buttons. Printing the pages as a PDF file was working fine on my test version at home, and initially seemed to work fine at work, but then I started getting intermittent VBA errors, sometimes 400 errors, sometimes 1004 errors.


My VBA skills are very much at the basic level - when I see it I can work out what it's doing but my skill level is more or less tweaking what I find in online tutorials and forums. On my copy of the spreadsheet at home, the code's in a module, and the buttons on the printable pages all are assigned to that same code block. Initially at work, I couldn't get it to work at all, was getting 400 errors, then 1004 errors. My VBA skills being basic I looked online to try and figure out what was going wrong, tried stepping through the code, and realised that the problem was with the filename, which I'd changed when I saved it on the system at work. Having tried it again it initially worked fine on one page, but couldn't get it to work on any of the other pages. I eventually got it to work by right-clicking on the tab of each page and pasting the code in for each page so I ended up with a macro for saving as a PDF for each sheet. That worked for a while on some pages, but then I started getting the same 400 and 1004 VBA errors. After looking into the problem again online I read somewhere that with these errors it's best to set up a new module and delete copies of the code set up at worksheet level. I did this and for 2 tries it worked, assigning the print button on each page to the same module, then suddenly stopped and the 400 and 1004 errors popped up every time I tried to print a worksheet as a PDF.


This setup, with a 'button' on each printable page assigned to the single macro is still working on my copy of the spreadsheet at home - I've tried it at home this evening, but the same setup on the copy at work is refusing to play. There doesn't seem to be a problem with the code itself, as it works intermittently, but not knowing that much about VBA I'm at a loss as to why it only works intermittently on the copy of the spreadsheet at work, but is working fine on my copy at home.

Does anyone have any suggestions as to what might be wrong?
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What line is your code failing on?
Are the buttons you are clicking on the same sheet that your data is on? If not this could cause an error with the way you currently have it set up.

You should be able to place this code in a single module and have as many buttons pointing to it that you want.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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