Print to PDF VBA

jhm0054

New Member
Joined
Jan 8, 2020
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I'm trying to create a macro to allow the print to PDF to have a shortcut key and save the excel sheet to the same folder and the workbook as the title of a particular cell in the active sheet. The current formula i am using is:
Sub save_excel_as_PDF()
With ActiveSheet.PageSetup
CenterHeader = "Sample Excel File Saved As PDF"
Orientation = xlPortrait
PrintArea = "$A$1:$F$40"
Zoom = False
FitToPagesTall = False
FitToPagesWide = 1
End With
ActiveSheet.ExportAsFixedFormat
Type:=xltypePDF
File:=activeworkbook.path"/"&activeSheet.range("B18").value,
Quality:=xlqualitystandard
ignoreprintarea:=false
From:=1,
To:=2,
Openafetrpublish:=true

End Sub

the final 8 lines of the code are causing me some issue as I continually get a compile error, expected: expression appears with the := highlighted

Can someone please check my code or provide a solution for this?

Cheer

J
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Firstly, inside a With block you need to prefix properties and methods with a "."

Secondly, if you're extending onto more than one line you need to use " _" at the end of each line

VBA Code:
Sub save_excel_as_PDF()
  With ActiveSheet.PageSetup
    .CenterHeader = "Sample Excel File Saved As PDF"
    .Orientation = xlPortrait
    .PrintArea = "$A$1:$F$40"
    .Zoom = False
    .FitToPagesTall = False
    .FitToPagesWide = 1
  End With

  ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B18").Value, _
    Quality:=xlQualityStandard, _
    IgnorePrintArea:=False, _
    From:=1, _
    To:=2, _
    OpenAfterPublish:=True
End Sub
 
Last edited:
Upvote 0
Thanks, Juddaaaa,

The code works, however, when I run the code I recieve a run-time error'13' type mismatch.

What dose this mean?
 
Upvote 0
There's one mistake I've spotted.

It's IgnorePrintAreas rather than IgnorePrintArea

VBA Code:
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B18").Value, _
    Quality:=xlQualityStandard, _
    IgnorePrintAreas:=False, _
    From:=1, _
    To:=2, _
    OpenAfterPublish:=True

but that wouldn't cause a Type Mismatch. That would be a Named Parameter error.

The only thing I can see that could cause a Type Mismatch is whatever is in cell B18.

Which line is the Debugger highlighting?
 
Upvote 0
There's one mistake I've spotted.

It's IgnorePrintAreas rather than IgnorePrintArea

VBA Code:
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B18").Value, _
    Quality:=xlQualityStandard, _
    IgnorePrintAreas:=False, _
    From:=1, _
    To:=2, _
    OpenAfterPublish:=True

but that wouldn't cause a Type Mismatch. That would be a Named Parameter error.

The only thing I can see that could cause a Type Mismatch is whatever is in cell B18.

Which line is the Debugger highlighting?
There's one mistake I've spotted.

It's IgnorePrintAreas rather than IgnorePrintArea

VBA Code:
ActiveSheet.ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B18").Value, _
    Quality:=xlQualityStandard, _
    IgnorePrintAreas:=False, _
    From:=1, _
    To:=2, _
    OpenAfterPublish:=True

but that wouldn't cause a Type Mismatch. That would be a Named Parameter error.

The only thing I can see that could cause a Type Mismatch is whatever is in cell B18.

Which line is the Debugger highlighting?

I was hoping the excel would save the PDF in the same location as the workbook and name the PDF the same as the title in B18.

is that what the Filename:=ActiveWorkbook.Path & "\" & ActiveSheet.Range("B18").Value, _ indicates?
 
Upvote 0
Use code tags around your code please.
Here is another possibility.
Code:
Sub Save_As_Pdf()
Dim PDF As String, a As String
With ActiveSheet
    .PageSetup.PrintArea = "A1:F40"
    a = .Range("B18").Value
End With
    PDF = ThisWorkbook.Path & "\" & a & ".pdf"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDF
End Sub
Change references where required.
 
Upvote 0
Thanks Guys for the help.

I'm having another issue now. I have recorded a shortcut key for the macro in the excel template however if I rename/move the file the macro no longer works. Is there a way to get the shortcut keys to work on the active sheet without having to record the shortcut again?
 
Upvote 0

Forum statistics

Threads
1,214,976
Messages
6,122,539
Members
449,088
Latest member
RandomExceller01

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