PDF print to file path VBA - excel

I have the below code I have researched and got to work in one of my workbooks. The idea of the code is to automatically print a sheet to PDF and save down in a directory of my choosing naming it the contents of a specific cell.
All works fine until I try to copy into a new workbook (I designed in a test workbook as not to corrupt the original) then I get a
run time error 1004
and the ThisWorkbook.Sheets(Array("sheet 2")).Select is highlighted when I debug. Am I missing something simple? as the code works in the original workbook but not in if I paste into a new module in a new workbook? Sorry i'm quite new to this!
sub PrintPDFRT()
Sheets("test").Activate
ActiveSheet.UsedRange.Select
Sheets("malbru1").Activate
ActiveSheet.UsedRange.Select
Sheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.Sheets(Array("sheet 2")).Select
Selection.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Try it like this: (without Array)
sub PrintPDFRT()
WorkSheets("test").Activate
ActiveSheet.UsedRange.Select
WorkSheets("malbru1").Activate
ActiveSheet.UsedRange.Select
WorkSheets("sheet 2").Activate
Range("A1:j137").Select
ThisWorkbook.WorkSheets("sheet 2").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="FIle path\" & Range("L7").Value, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub

Related

Excel VBA: Save active worksheet as PDF

I've been using some code to save an active worksheet to PDF, using the current date/time as the filename, and saving into a SharePoint folder.
Sub Save()
Dim strFilename As String
strFilename = Format(Now(), "yyyy-mm-dd hhmm")
Sheets("Weather Warning Action Sheet").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"https://xxx.sharepoint.com/sites/xxx/xxx/Paperwork and Admin/WX Warnings/2020/" & strFilename & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
End Sub
This code has been working fine for a while, but has suddenly come up with the "Run-time error '1004': Document not saved" error message.
Debugging shows this as the error:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"https://xxx.sharepoint.com/sites/xxx/xxx/Paperwork and Admin/WX Warnings/2020/" & strFilename & ".pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
Any assistance would be greatly appreciate.
Many thanks,
James

How do I fit my data to one sheet when exporting to pdf in vba?

I am trying to export a specific range on one of my excel sheets to pdf. It cuts off a small part at the bottom and puts it on the next page. My question is : How do I change my code below to allow for a "fit to one page" option and how do I make the orientation landscape?
Sub printdispatchsheet()
Sheets("DispatchSheet").Range("A1:J48").ExportAsFixedFormat
Type:=xlTypePDF, fileName:= _
"c:\Users\name\Desktop\DispatchSheet.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, _
OpenAfterPublish:=True
End Sub
Could you try and see if this works? Also see if setting your print areas properly helps, if not modify that part of the code to True
Sub printdispatchsheet()
With Sheets("DispatchSheet")
.PageSetup.Orientation = xlLandscape
.PageSetup.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 1
End With
Sheets("DispatchSheet").Range("A1:J48").ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="c:\Users\name\Desktop\DispatchSheet.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub

Saving Range Selection as PDF on Mac

I have the following code to try and export a specific selection as a one-page PDF. I was able to grant access but I can't seem to be able to export the selection as a PDF. I get a 1004 error. Any advice?
Sub PrintTest()
ChDir "/Users/Mr_Madoff/Desktop/"
Sheets("Sheet1").Range("A1:F53").Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Mr_Madoff/Desktop/Book1.pdf", Quality:=xlQualityMinimum, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
End Sub

Saving worksheets with forms as pdf

I'm trying to save a worksheet as PDF and I was successful at doing this, but when I open the generated PDF file all the textboxes in the sheet are deformed.
How can I keep the ratio of the exported textboxes?
I'm using a button in the worksheet that executes the following code:
ThisWorkbook.Sheets(1).Select
Sheets(1).Activate
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:="\\10.18.105.44\Setor Proteção RDs\01 - Ordens de Ajustes\Banco de ordens\AL" & alimentador & "\" & nome_arquivo & ".pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
Sheets(1).Select
Range("D2").Select
I discovered that this only occurs when i try to save with the zoom of the workbook larger than 100%, when i print with zoom at 100% it goes like a charm.

Save Excel range as pdf

I'd like to 'save as' a specific sheet or specific range to pdf.
I tried implementing a range into my code.
Here is what I've been working with:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"c:\Book1.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
you have the code, just use a range instead of activesheet
e.g. Sheets("Sheet1").Range("B2:H28").ExportAsFixedFormat ...

Resources