Issue Printing to PDF using a Command Button and VBA

PandaPlaza1277

New Member
Joined
Aug 2, 2022
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Hello,

I'm running into an issue printing a specific hidden spreadsheet to PDF using a Command Button. I've managed to make this code work in another workbook of mine and I've even had this code work a number of times in this spreadsheet (maybe 4 times or so earlier on). For some reason, it does not consistently work. I've spent a good amount of time searching for answers and verifying the code that seems to be in question, but as far as I can tell, the input is correct. The error seems to be occurring with the "OpenAfterPublish:=True" line of code. Does anyone have any ideas what could be going wrong?
 

Attachments

  • Print Summary to PDF.jpg
    Print Summary to PDF.jpg
    98.6 KB · Views: 12

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
My first thought is that you have the pdf open and trying to run the code again will result in error.
 
Upvote 0
My first thought is that you have the pdf open and trying to run the code again will result in error.
Unfortunately, no. I don't have the PDF open at all. I've even tried closing out my PDF viewing program (Bluebeam) completely, just in case.
 
Upvote 0
Try running the code again. This time, though, when the error occurs, enter the following line in the Immediate Window (Ctrl+G), and press ENTER...

VBA Code:
? dir(pdfile, vbNormal)

If the path and filename are correct, it should return the filename. Does it return the filename?
I think I did this correctly. In the Immediate Window, I typed, "? dir(pdfile, vbNormal)". This brought up the error: Run-time '52': Bad file name or number.
 
Upvote 0
Check to make sure that path and filename does not contain more than the maximum characters allowed, which in newer versions is 218 characters, as per Excel specifications and limits.

Also check to make sure that the filename does not contain any illegal characters, such as the following...

VBA Code:
\
/
<
>
?
[
]
:
|
*
"
 
Last edited:
Upvote 0
Solution
Check to make sure that path and filename does not contain more than the maximum characters allowed, which in newer versions is 218 characters, as per Excel specifications and limits.

Also check to make sure that the path and filename does not contain any illegal characters, such as the following...

VBA Code:
<
>
?
[
]
:
|
*
"
Well, the file path contains roughly 46 characters. The only special character in the path is "\" and the only special character in the filename is " - ", which from previous experience in a different workbook, didn't have any problems.
 
Upvote 0
When the error occurs, enter the following line in the Immediate Window...

VBA Code:
? pdfile

What does it return?
 
Upvote 0
When the error occurs, enter the following line in the Immediate Window...

VBA Code:
? pdfile

What does it return?
Hm... It almost returns the correct output that I'm looking for. Almost. It is displaying (skipping a little bit): "...\Desktop\ - Summary Analysis - .pdf".
It looks like it's not putting in the "SumAn.Range("C9").Value" or the "SumAn.Range("C10").Value".
 
Upvote 0
Do those cells contain any values?
Yes, they're supposed to contain the values "301600" currently, which is typed into cell C9 on the Summary Analysis page, and the second value is "8/4/22" currently, which is typed into cell C10 on the Summary Analysis page.
 
Upvote 0

Forum statistics

Threads
1,215,558
Messages
6,125,504
Members
449,235
Latest member
Terra0013

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