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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.

Sequoyah

Board Regular
Joined
Mar 26, 2017
Messages
66
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
 

PuruBhati

New Member
Joined
Sep 23, 2021
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

Sequoyah

Board Regular
Joined
Mar 26, 2017
Messages
66
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
 

PuruBhati

New Member
Joined
Sep 23, 2021
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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.
 

Forum statistics

Threads
1,147,747
Messages
5,742,964
Members
423,769
Latest member
LongToast

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
Top