VBA Save As PDF

toughie

New Member
Joined
Oct 10, 2018
Messages
43
Hi im looking for simple code to use vba to save an excel file as a PDF ?

Here is my code so far :


Set wb = Workbooks.Add
ThisWorkbook.Activate
ActiveSheet.Copy Before:=wb.Sheets(1)
wb.Activate
wb.SaveAs Filename:= Range("G5") & "-" & Range("G7") & Range("D7") & Range("N1")

Any help will be appreciated thanks
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi Toughie

The following will assist you:

Code:
Sub SavePDF()

Dim PdfFile As String


' Define PDF filename and path
  PdfFile = "C:\Temp\Filename.pdf"
' Export activesheet as PDF
  With ActiveSheet
    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
  End With


End Sub
 
Upvote 0
If Val(ComboBox5.Value) = Range("K8") = True Then



PdfFile = "M:\bot\macdata\Vam Cell\VAM LOG" & Range("G5") & "-" & Range("G7") & Range("D7") & Range("N1")
With ActiveSheet
.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PdfFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
wb.Activate
Else
MsgBox "Incorrect Revision"
Exit Sub

I have this code now and i cant get it to run without getting error else without if

any idea
 
Upvote 0
Thanks for the help Prish that worked

I get save type method failed error when I use this code now any idea whats wrong with this?

wb.SaveAs Filename = "C:\Users\andy_\OneDrive\Desktop\VAMLog" & Range("G5") & "-" & Range("G7") & Range("D7") & Range("N1") & ".xlsm"
 
Upvote 0
The syntax for your named parameter is incorrect. It should be Filename:=" . . . " . By the way, you should specify the file format as well. So it should be...

Code:
wb.SaveAs Filename:="C:\Users\andy_\OneDrive\Desktop\VAMLog" & Range("G5") & "-" & Range("G7") & Range("D7") & Range("N1") & ".xlsm", FileFormat:=52 'xlOpenXMLWorkbookMacroEnabled

Hope this helps!
 
Last edited:
Upvote 0
after Exit Sub

Putting it there doesn't make sense because then the With ... End With is spread between the If and Else blocks.

I would put the End With as close to the With as possible, i.e. immediately after the .ExportAsFixedFormat line.
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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