VBA Code to Save PDF file to location as defined in a cell

Carl Stephens

New Member
Joined
Jan 3, 2017
Messages
22
Hello All,

I have the below code that prints to pdf and gives the file name based on cell N1, and in cell N2 I have the file path location of where I want the file to save, but for the life of me cannot get the code right, and I am hoping that someone can kindly enlighten me on what the code should look like. Thank you all.

Sub PrintVisa()

s = Range("N1").Value
'
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
s, Quality:=xlQualityStandard, IncludeDocProperties _
:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

MsgBox "Success - Ensure that you close the PDF file before printing another document."

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Try this:

VBA Code:
Sub PrintVisa()
  Dim s As String, p As String
  
  s = Range("N1").Value   'file name
  p = Range("N2").Value   'path
  If Right(p, 1) <> "\" Then p = p & "\"
  If Dir(p, vbDirectory) = "" Then
    MsgBox "The path does not exist"
    Exit Sub
  End If
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=p & s, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

  MsgBox "Success - Ensure that you close the PDF file before printing another document."
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub PrintVisa()
  Dim s As String, p As String
 
  s = Range("N1").Value   'file name
  p = Range("N2").Value   'path
  If Right(p, 1) <> "\" Then p = p & "\"
  If Dir(p, vbDirectory) = "" Then
    MsgBox "The path does not exist"
    Exit Sub
  End If
  ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=p & s, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

  MsgBox "Success - Ensure that you close the PDF file before printing another document."
End Sub
Thank you. I got an run-time error 53 - file not found on the below code. Do you know what the error is?

Sub PrintVisa()
Dim s As String, p As String

s = Range("N1").Value 'file name
p = Range("N2").Value 'path
If Right(p, 1) <> "\" Then p = p & "\"
If Dir(p, vbDirectory) = "" Then
MsgBox "The path does not exist"
Exit Sub
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=p & s, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

MsgBox "Success - Ensure that you close the PDF file before printing another document."
End Sub
 

Attachments

  • 1670375815637.png
    1670375815637.png
    214.2 KB · Views: 6
Upvote 0
Thank you. I got an run-time error 53 - file not found on the below code. Do you know what the error is?

Sub PrintVisa()
Dim s As String, p As String

s = Range("N1").Value 'file name
p = Range("N2").Value 'path
If Right(p, 1) <> "\" Then p = p & "\"
If Dir(p, vbDirectory) = "" Then
MsgBox "The path does not exist"
Exit Sub
End If
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=p & s, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True

MsgBox "Success - Ensure that you close the PDF file before printing another document."
End Sub
Same error again......sorry to be a pain.
 
Upvote 0
You can put here what you have inside cell N2
And also check that the folder exists on your computer
 
Upvote 0
You can put here what you have inside cell N2
And also check that the folder exists on your computer
I have a One Drive address in the cell, and I have just changed the address to my desktop and it works, so the issue is with the One Drive address. Thank you for making the code work, now I need to resolve the One Drive path issue.
 
Upvote 0
I have a One Drive address in the cell, and I have just changed the address to my desktop and it works, so the issue is with the One Drive address. Thank you for making the code work, now I need to resolve the One Drive path issue.

This is what is in cell N2. The stars are to hide my account details. I checked and the folder exists on my One Drive account. I am using the http address rather then the local path, which seems to be the issue.
Thank you.

h t t p s://carnivalcorp-my.sharepoint.com/personal/****/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fcarl%5Fstephens%5Fseabourn%5Fco%5Fuk%2FDocuments%2FNew%20Trackers%20%2D%20HA%20Group%2FSeabourn%2FVisa%20Letters
 
Upvote 0
This is what is in cell N2. The stars are to hide my account details. I checked and the folder exists on my One Drive account. I am using the http address rather then the local path, which seems to be the issue.
Thank you.

h t t p s://carnivalcorp-my.sharepoint.com/personal/****/_layouts/15/onedrive.aspx?id=%2Fpersonal%2Fcarl%5Fstephens%5Fseabourn%5Fco%5Fuk%2FDocuments%2FNew%20Trackers%20%2D%20HA%20Group%2FSeabourn%2FVisa%20Letters


Would you be able to advise instead, on what the code will look like after saving to pdf, to then save to the desktop, no matter who the user is (as there are multiple users). I am thinking that the below code needs to be inserted somewhere? But, I am not too sure, as I am fairly new to coding. Thank you.

user = Environ("Username")
desktop = "C:\Users\" & user & "\Desktop\"
 
Upvote 0
Change this line:

p = Environ("USERPROFILE") & "\Desktop\"
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,259
Members
449,075
Latest member
staticfluids

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