VBA | Save Excel Sheets as PDF and keep Excel File Name and Path

mjb342

New Member
Joined
May 3, 2020
Messages
11
Office Version
  1. 365
Platform
  1. Windows
Hello,

I’m trying to save specific sheets defined by the user in a Table to PDF. I want the PDF to have the same name as the excel file minus the extension. I also want it saved to the same directory as where the excel file resides. It seems to work, but if I move a copy of the file to a new location it saves the PDF in the original file location and not where a copy of the excel file was moved to.

Any help is greatly appreciated.


VBA Code:
Public Sub CreatePDF()
Dim TOCTable1 As ListObject
Dim PDFSheets() As String
Dim c As Byte 'number of tabs to be exported
Dim FileName As String
Dim FileOnly As String
Dim filePath As String

On Error GoTo Handle:

FileOnly = ThisWorkbook.Name
filePath = ThisWorkbook.FullName
FileName = FileOnly
If InStr(FileName, ".xls") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
Set TOCTable1 = Worksheets("Index").ListObjects("TOCTable1")
ReDim PDFSheets(1 To TOCTable1.DataBodyRange.Rows.Count)
'fill up the array
For c = 1 To UBound(PDFSheets)
PDFSheets(c) = TOCTable1.DataBodyRange(c, 1).Value
Next c
Worksheets(PDFSheets).Select
ActiveSheet.ExportAsFixedFormat xlTypePDF, FileName
Worksheets("Index").Select
MsgBox "PDF file was created." & vbNewLine & "File is called Provision. It is saved on the same directory as this workbook.", , "Well Done"
Exit Sub

Handle:
If Err.Number = 9 Then
MsgBox "It looks like a tab name was not spelled correctly. Please double check."
Else
MsgBox "Looks like error here. Please ensure sheets are visible..."
End If

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi mjb342,

Try this:

VBA Code:
Option Explicit
Public Sub CreatePDF()

    Dim TOCTable1 As ListObject
    Dim PDFSheets() As String
    Dim c As Byte 'number of tabs to be exported
    Dim strFullPath As String, strFileName As String, strPath As String
    
    On Error GoTo Handle:
    
    strFullPath = ThisWorkbook.FullName
    strFileName = Left(Dir(strFullPath), InStr(Dir(strFullPath), ".") - 1)
    strPath = Replace(strFullPath, Dir(strFullPath), "")
    
    Set TOCTable1 = Worksheets("Index").ListObjects("TOCTable1")
    ReDim PDFSheets(1 To TOCTable1.DataBodyRange.Rows.Count)
    'fill up the array
    For c = 1 To UBound(PDFSheets)
        PDFSheets(c) = TOCTable1.DataBodyRange(c, 1).Value
    Next c
    Worksheets(PDFSheets).Select
    ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & strFileName
    Worksheets("Index").Select
    MsgBox "PDF file was created." & vbNewLine & "File is called Provision. It is saved on the same directory as this workbook.", , "Well Done"
    Exit Sub
    
Handle:
    If Err.Number = 9 Then
    MsgBox "It looks like a tab name was not spelled correctly. Please double check."
    Else
    MsgBox "Looks like error here. Please ensure sheets are visible..."
    End If

End Sub

Regards,

Robert
 
Upvote 0
Solution
It seems to work, but if I move a copy of the file to a new location it saves the PDF in the original file location and not where a copy of the excel file was moved to.

Replace:
VBA Code:
FileOnly = ThisWorkbook.Name
filePath = ThisWorkbook.FullName
FileName = FileOnly
If InStr(FileName, ".xls") > 0 Then
FileName = Left(FileName, InStr(FileName, ".") - 1)
End If
with:
VBA Code:
    Filename = Left(ThisWorkbook.FullName, InStrRev(ThisWorkbook.FullName, ".")) & "pdf"
 
Upvote 0
Hi mjb342,

Try this:

VBA Code:
Option Explicit
Public Sub CreatePDF()

    Dim TOCTable1 As ListObject
    Dim PDFSheets() As String
    Dim c As Byte 'number of tabs to be exported
    Dim strFullPath As String, strFileName As String, strPath As String
   
    On Error GoTo Handle:
   
    strFullPath = ThisWorkbook.FullName
    strFileName = Left(Dir(strFullPath), InStr(Dir(strFullPath), ".") - 1)
    strPath = Replace(strFullPath, Dir(strFullPath), "")
   
    Set TOCTable1 = Worksheets("Index").ListObjects("TOCTable1")
    ReDim PDFSheets(1 To TOCTable1.DataBodyRange.Rows.Count)
    'fill up the array
    For c = 1 To UBound(PDFSheets)
        PDFSheets(c) = TOCTable1.DataBodyRange(c, 1).Value
    Next c
    Worksheets(PDFSheets).Select
    ActiveSheet.ExportAsFixedFormat xlTypePDF, strPath & strFileName
    Worksheets("Index").Select
    MsgBox "PDF file was created." & vbNewLine & "File is called Provision. It is saved on the same directory as this workbook.", , "Well Done"
    Exit Sub
   
Handle:
    If Err.Number = 9 Then
    MsgBox "It looks like a tab name was not spelled correctly. Please double check."
    Else
    MsgBox "Looks like error here. Please ensure sheets are visible..."
    End If

End Sub

Regards,

Robert


Hello Robert,

I just tested and it worked perfectly.

Thank you for the solution. Greatly appreciated.

Mike
 
Upvote 0
Hello Robert,

I just tested and it worked perfectly.

Thank you for the solution. Greatly appreciated.

Mike

Thanks for letting us know and you're welcome :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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