saving as PDF

rickblunt

Well-known Member
Joined
Feb 18, 2008
Messages
609
Office Version
  1. 2019
Platform
  1. Windows
Hello, Below is a snippet of code that I am using in a routine to save a worksheet to a back-up drive, and it all works fine. On my last audit the auditor "suggested" that I should be saving these files in a format that cannot be altered, such as PDF. Does anyone have a suggestion on how I can tweak this code to save it as a pdf instead of xls? There doesn't seem to be a value that does this in "FileFormat"
Thanks for any input

VBA Code:
Sheets("Fall Protection Report").Copy                
        Set wb = ActiveWorkbook
       With wb
            Application.DisplayAlerts = False
            .SaveAs _
            "\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
            & Format(Now, "yy_mmdd") & ".xlsx", FileFormat:=51
            .Close True
        Application.DisplayAlerts = True
        
     End With
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
To export your worksheet to PDF, you can use the ExportAsFixedFormat method of the Worksheet object . . .

VBA Code:
    Sheets("Fall Protection Report").ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
            & Format(Now, "yy_mmdd") & ".xlsx"

This method contains other parameters that can be set. Have a look at the following link for more information . . .


Hope this helps!
 
Upvote 0
Solution
Thanks Domenic for the help, the file does get saved to the folder as a pdf file, but it cannot be opened - it appears to be corrupted. Not sure if maybe there is some items in my worksheet (like userforms or macros) that is confusing the pdf reader? But when I save the file directly from excel as a PDF it works fine. It must be somewhere later in that particular sub that is creating the issue, as the sub does not completely finish. But you did answer my question so I really appreciate the solution, thanks for the help.
 
Upvote 0
I think the issue is that you are exporting to PDF, but the file extension is still "xlsx". That could be causing the corruption message (when file format and extension do not match up).

Try changing it to:
Rich (BB code):
  Sheets("Fall Protection Report").ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
            & Format(Now, "yy_mmdd") & ".pdf"
 
Upvote 0
I think the issue is that you are exporting to PDF, but the file extension is still "xlsx". That could be causing the corruption message (when file format and extension do not match up).

Try changing it to:
Rich (BB code):
  Sheets("Fall Protection Report").ExportAsFixedFormat _
        Type:=xlTypePDF, _
        Filename:="\\MESSDV002.na.infineon.com\Facilities\DeptData\EH&S\Compliance and EHS\Facilities Inspections\Fall Protection\Fall Protection Inspection Report  " _
            & Format(Now, "yy_mmdd") & ".pdf"
Yes, Joe4 you are correct there, I got an error when I first ran it, but when I changed it to pdf it worked better. Or at least it dropped a pdf file in the folder, but that is about where the code stopped running... I'm going through my entire code now to figure out what is hanging up. Thanks for the input
 
Upvote 0
Yes, Joe4 you are correct there, I got an error when I first ran it, but when I changed it to pdf it worked better. Or at least it dropped a pdf file in the folder, but that is about where the code stopped running... I'm going through my entire code now to figure out what is hanging up. Thanks for the input
You are welcome.

Yep, sounds like you have an unrelated issue going on (at least, unrelated to the code you posted).
You may to try stepping through your code to find out where it is hanging.
 
Upvote 0
Thanks for the help - I don't see an issue with why its hanging up where it is, should I create a new thread or just continue it here?
 
Upvote 0
Thanks for the help - I don't see an issue with why its hanging up where it is, should I create a new thread or just continue it here?
Since it is a different question/matter, you should start a new thread.
 
Upvote 0
That's what I thought :) I want to try my hardest to figure it out before I ask for help though, thanks again for the input...
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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