Macro to Save as PDF - Using:Worksheet Name, Path, & file name in cells

MarkReddell

Board Regular
Joined
Sep 1, 2011
Messages
210
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Hello to Anyone who can Please help,

I need to Export to a PDF in the cell Names: 1. Path is cell names - "OneDriveFilePath, & "PCFilePath". 2. File Name is: "FileName". Thx. 4 any help!!! :confused::confused::confused:
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
This should be a pretty good start point


Code:
Sub MyCreatePDF()
'
' PDF generating Macro
'

On Error GoTo MyCreatePDFErr
    
    Dim strTheFileSaveName As String
    TheFileName = "File_" & ActiveWorkbook.Names("MyNamedRange").RefersToRange & ".PDF"
        'open file save dialog to create a apth and name to save the PDF to
'        strTheFileSaveName = Application.GetSaveAsFilename(InitialFileName:=ActiveWorkbook.Names("PDF_Path").RefersToRange & "HSS_" & ActiveWorkbook.Names("MyNamedRange").RefersToRange & "_Txn", _
'        filefilter:="PDF (Adobe PDF) (*.pdf), *.Pdf", Title:="Please enter PDF filename to export to")
'
'        'pressed the cancel button
'        If strTheFileSaveName = "False" Then
'            MsgBox "PDF creation aborted  no file will be produced ", , "CMF Trade PDF Creation"
'            Exit Sub
'        End If
        
        
        Application.ScreenUpdating = False
        
        'in order to produce the PDF the sheet has to be visible
        
        ActiveWorkbook.Sheets("pdf").Visible = True
        
        ActiveWorkbook.Sheets("pdf").Select
            
        ActiveWorkbook.Sheets("pdf").Range("B3:E33").Activate  ' area of PDF
        
        ' system footer  HSBC
        With ActiveSheet.PageSetup
            .LeftFooter = "RESTRICTED - &D" & " " & "&T" & "    " & varVersionStamp
        End With
        
        'produce the PDF from the sheet
        
        
'        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTheFileSaveName, _
'            Quality:=xlQualityMinimum, IncludeDocProperties:=False, IgnorePrintAreas:=True, OpenAfterPublish:=True
        
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=TheFileName, _
            Quality:=xlQualityMinimum, IncludeDocProperties:=False, IgnorePrintAreas:=True, OpenAfterPublish:=True
        DoEvents
        
        'save the file name
        gblstrPDFName = strTheFileSaveName
        DoEvents
        
                
        ActiveWorkbook.Sheets("pdf").Visible = False
        
        DoEvents
        
        Application.ScreenUpdating = True
        
        Exit Sub
    
MyCreatePDFErr:
    
        MsgBox "This error : " & Err & " :" & Error & vbNewLine & vbNewLine & " This has stopped the PDF production process " & _
        vbNewLine & vbNewLine & " Please contact your system Administrator or support function.", vbCritical, "PDF Creation"
        
        Application.ScreenUpdating = True
        
        Exit Sub
        
    End Sub







Ps
"9 If you declare with your mouth, “Jesus is Lord,” and believe in your heart that God raised him from the dead, you will be saved."

Is that to your C: or to a network drive?
 
Upvote 0
Thanx Mr. Chuckie!!! Wow! Your code is way above me! Maybe "U" can modify it with the info. below. If not, maybe "U" can help me modify my macro below! Thanx again!!!

This macro below works ok, but I want to be able to export a PDF in two paths. The 1st cell ref. is in W/S "OrderForm", cell "AF1" which is named, "OneDriveFilePath", & the 2nd is in "AF2" which is named: "PCFilePath". The file name to save under is in: W/S - "OrderForm" in cell "AG1" which is named "FileName".

CODE:

Sub Print2PDF()
'
' Print2PDF Macro
'


'
ActiveSheet.ListObjects("Table8").Range.AutoFilter Field:=13, Criteria1:= _
"<>"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"https://d.docs.live.net/a7f2fe19067884e3/Reddy%20Ice/Parts%20Order%20Form.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,172
Members
449,071
Latest member
cdnMech

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