Excel VBA macro Print to PDF - output file not supported? - vba

I have a simple macro that selects a sheet, enters basic data then prints to a PDF file. The macro works and the file is created, however, when trying to open the resulting file, an error comes up on Adobe Acrobat saying 'Acrobat could not open 'the document' because it is either not a supported file type or because the file has been damaged.
Here's my code (names and text changed to protect the innocent)
Sheets("Sheet1").Select
ActivePrinter = "CutePDF Writer on CPW2:": _
Range("Ab2").Select
ActiveCell.FormulaR1C1 = "text 001"
Range("Ab3").Select
ActiveCell.FormulaR1C1 = "text 002"
Range("Ab4").Select
ActiveCell.FormulaR1C1 = "text 003"
ActiveWindow.SelectedSheets.PrintOut _
printtofile:=True, Collate:=True, prtofilename:="filename.pdf"
If I comment out the ActivePrinter line and change the filename to a .ps file, it will create a postscript file that opens just fine in Acrobat. I know I can then convert the .ps file to .PDF, but I'm trying to avoid that since the macro runs thru 150+ variations creating 150+ files.
Has anyone had similar experiences, and if so, what did you do to overcome them?
Any assistance would be greatly appreciated.

This is the code (from macro recorder) that will save a file as PDF:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Temp\Sample.pdf", Quality:= xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Related

VBA - Excel sheet wont save to current directory - defaults to My Documents

I want to save 2 sheets of a xlsm file as MSDOS text filea with a .bat extension so I can run them as scripts, and have it save them to the current directory. What would cause it to default to My Documents instead?
I'm not a coder - I used Record Macro to create it and then edited it to take out the specific path. I actually had this working until I added the ActiveWorkbook.Save at the beginning, and the ActiveWorkbook.Close SaveChanges:=False at the end so I wouldn't lose the formatting by accidentally saving it manually. Now it saves the files to My Documents no matter what I try. I can't imagine that would cause it, and taking those 2 lines out didn't fix it. What am I missing?
Here is my code:
ActiveWorkbook.Save
Sheets("Copy Script").Select
ActiveWorkbook.SaveAs Filename:= _
"copy.bat", FileFormat:= _
xlTextMSDOS, CreateBackup:=False
Sheets("Plot Script").Select
ActiveWorkbook.SaveAs Filename:= _
"plot.bat", FileFormat:= _
xlTextMSDOS, CreateBackup:=False
ActiveWorkbook.Close SaveChanges:=False
Try this
ActiveWorkbook.SaveAs Filename:= ThisWorkbook.Path & "\copy.bat", FileFormat:= xlTextMSDOS, CreateBackup:=False

Saving a PDF automatically to a predetermined folder

I am writing a macro that automates a daily report that I send out each day. One of my last items is to write a script that saves the formatted excel sheet as a PDF (through a print to pdf option), and save it in a specific folder. I have the following written, however, it still prompts the user as to where to save it.
What would be a better way, or a way in general, to have it save automatically to a folder somewhere on my desktop?
Sub printToPDF()
Worksheets("general_report").PageSetup.CenterVertically = False
ActiveWindow.SelectedSheets.PrintOut Copies:=1, ActivePrinter:="Foxit Reader PDF Printer"
End Sub
This should do the work,
Sub printToPDF()
Dim FilePath As String
Dim FileName As String
FilePath = "C:\Users\userName\Desktop\" 'Change as per your username
ActiveSheet.Copy 'Copy a worksheet to a new workbook
'It saves .PDF file at your Descrop with the name of the worksheet
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FilePath & ActiveSheet.Name, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'Closing a newly created workbook without saving it
Application.DisplayAlerts = False
ActiveWorkbook.Close
End Sub

VBA Exporting PDF from Excel in 2 copies

I'm looking for a solution to export couple of sheets from Excel to one file PDF. I've recorded a macro that creates nice PDF with all interesting me sheets. BUT i need 2 copies of one of the sheets in the same PDF, but I don't know how to do it.
Here is my code.
Sub ExportPDF()
Sheets(Array("PackingList", "Administracyjny", "Nadawca", "Odbiorca", "Przewoźnik")).Select
Sheets("PackingList").Activate
ChDir "C:\Users\XXXXXX\Desktop"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\XXXXXX\Desktop\Spools_PackingList.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:= False
End Sub
See here for copying a worksheet, it might be a good workaround to copy the entire worksheet at the start of your code, and then you can just delete it at the end with (taken from here):
Sub sbDeleteASheet()
Sheet1.Delete
'OR You can mention the Sheet name
Sheets("Sheet2").Delete
End Sub

Exporting excel sheet as PDF Run-time error '5': Invalid procedure call or argument

I have a problem exporting one Excel sheet in a workbook into a PDF file.
Sub ExportPDF()
Dim Nazwa As String
' Creating a destination folder for PDF file
If Len(Dir("c:\Faktury", vbDirectory)) = 0 Then
MkDir "c:\Faktury"
End If
'InputBox that allows user to type filename
Nazwa = InputBox("Wpisz nazwę pliku", "Nazwa pliku", Sheets("Fa VAT").Range("G3"))
If Nazwa = "" Then Exit Sub
ChDir "c:\Faktury"
'Exporting sheet "Fa VAT" to PDF
Sheets("Fa VAT").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Nazwa, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
I get the error "Run-time error '5': Invalid procedure call or argument". After that Excel highlights the last part of the code as it is supposed to be wrong. I mean exactly this part:
Sheets("Fa VAT").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=Nazwa, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
I installed Microsoft SaveAsPDF setup and nothing changes. I changed filename to some random string rather than a referring to InputBox. I tried to activate the right sheet and then export ActiveSheet. I tried with the sheet hidden and shown. I tried to run the file on two other computers.
When I copy this macro to some completely new, blank excel spreadsheet, it works.
I work on Excel 2007, Windows 10. I tried it also on Excel 2010 and Windows 7.
Here are some of the links that I tried:
Runtime Error : 5, Invalid Procedure call or argument
http://www.mrexcel.com/forum/excel-questions/595252-visual-basic-applications-trouble-save-pdf-code.html
Make sure the worksheet isn't hidden. Excel will throw an error for .ExportasFixedFormat if the worksheet is hidden or very hidden. If yours is hidden, you can unhide it first in your code:
Sheets("Fa VAT").Visible = xlSheetVisible
At the end, you can hide it again.
Sheets("Fa VAT").Visible = xlSheetHidden
I had the same error. In my case, the filepath and name given were too long. Shorten the name or path and it should work.

VBA ExportAsFixedFormat only saving last active chart

I'm trying to save a single worksheet from a workbook as a pdf using the ExportAsFixedFormat method:
Sheets("Overview").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Mid(saveFile, 1, InStr(saveFile, ".")) & "pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
This worked for a while until i started doing some chart manipulation beforehand. The manipulation I am talking about looks like this:
ActiveSheet.ChartObjects("Diagramm 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Measurements").Range( _
"C4:C29,G4:G29")
Now its not exporting the whole sheet as a pdf but rather only the chart called "Diagramm 4".
I more or less understand why it's doing this but I can't find a way to fix this.
You could try selecting any cell on that sheet like:
Range("A1").Select
before you export the page. Likely this is happening because you're making the chart active without making it inactive again. Look what happens when you normally select a chart and then try to print a worksheet - it'll just try to print the chart.