Active workbook file name in PDF - Macro

PuruBhati

New Member
Joined
Sep 23, 2021
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hello - I am trying to save multiple tabs in to a PDF from excel and below is the macro I am using. Macro works fine but would like to add active workbook file name so I don't have to retype the name.

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= Application.GetSaveAsFilename, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas :=True, OpenAfterPublish:=True
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Hy PuruBhati,
try this code:
VBA Code:
Sub test()

    Dim MyFile As String
    Dim fso As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    MyFile = ThisWorkbook.Path & "\" & fso.GetBaseName(ActiveWorkbook.Name) & ".pdf"
      
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFile, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

End Sub
 
Upvote 0
Hy PuruBhati,
try this code:
VBA Code:
Sub test()

    Dim MyFile As String
    Dim fso As Object
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    MyFile = ThisWorkbook.Path & "\" & fso.GetBaseName(ActiveWorkbook.Name) & ".pdf"
     
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFile, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True

End Sub
Hey Sequoyah - Thank you for the code but it did not work. this is part of a larger macro and all I want is when the last piece of code runs when it is saving the file in to the PDF format, I want the workbook name appear as default rather than me typing.

Let me know this makes sense.
 
Upvote 0
Hi PuruBhati,
here's the correct version
VBA Code:
Sub test2()
    
    Dim MyFile As Variant
    Dim fso As Object
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    MyFile = Application.GetSaveAsFilename(InitialFileName:=fso.GetBaseName(ActiveWorkbook.Name), _
             FileFilter:="PDF Files (*.pdf), *.pdf", _
             Title:="Select Path And Filename")
    
    If MyFile <> False Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFile, _
                                        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True
    End If
    
End Sub
 
Upvote 0
Hi PuruBhati,
here's the correct version
VBA Code:
Sub test2()
   
    Dim MyFile As Variant
    Dim fso As Object
   
    Set fso = CreateObject("Scripting.FileSystemObject")
   
    MyFile = Application.GetSaveAsFilename(InitialFileName:=fso.GetBaseName(ActiveWorkbook.Name), _
             FileFilter:="PDF Files (*.pdf), *.pdf", _
             Title:="Select Path And Filename")
   
    If MyFile <> False Then
        ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=MyFile, _
                                        Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=True
    End If
   
End Sub
Thank you. It worked.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,849
Members
452,361
Latest member
d3ad3y3

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