PDA

View Full Version : [SOLVED] Trouble saving as PDF



kualjo
01-07-2016, 07:25 AM
I am trying to create individual files from a master file based on a selection variable. Once those files are created, I want to save them in PDF format. I'm using the following code :

Sheets(k).Select
Sheets(k).Copy
ActiveWorkbook.SaveAs Filename:=FolderName & "/" & ActiveSheet.Name, FileFormat:=xlTypePDF, CreateBackup:=False
ActiveWorkbook.Save
ActiveWorkbook.Close

I was originally using FileFormat:=xlOpenXMLWorkbook, and that worked fine. Changing it to PDF, however, causes an error. Is there something I need to modify in order to accommodate the change to PDF?

mancubus
01-07-2016, 09:01 AM
please use code tags when posting your code.

worked for me:



Sheets(k).Copy
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderName & "\" & ActiveSheet.Name
ActiveWorkbook.Close False

Kevin#
01-07-2016, 09:03 AM
This avoids need for the last 2 lines and you need only select the sheet not copy it.

Note
-the way the "\" leans NOT "/"
- sheet name should be in " " ie sheets("k") NOT sheets(k)
(unless it is set up as a string variable)



Sheets("k").Select

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FolderName & "\" & ActiveSheet.Name & ".pdf"

Loos like I was beaten to the same draw bymancubus (http://www.vbaexpress.com/forum/member.php?37987-mancubus)

kualjo
01-07-2016, 11:17 AM
Mancubus, your suggestion worked. Thanks! (Apologies for not tagging the code; got ahead of myself.)

Kevin: Yes, the 'k' is a variable name.

mancubus
01-07-2016, 03:12 PM
you are welcome. thanks for marking the thread as solved.