简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM