prompting user to save excel activesheet as pdf but first choosing folder: Code Insid

Abouya

Registered User.
Local time
Today, 15:39
Joined
Oct 11, 2016
Messages
88
Hello,

I don't get why this code doesn't work, could someone help:

Code:
Sub saveaspdf()

Dim TempFileName As String

'Prompt for file destination
    With Application.FileDialog(msoFileDialogFolderPicker)
        
        If .Show = True Then
        
            DestFolder = .SelectedItems(1)
            
        Else
        
            MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
                
            Exit Sub
            
        End If
        
    End With

Set wb1 = ActiveWorkbook

TempFileName = Replace(wb1.Name, ".xlsm", "") & " #" & Range("H5") & " " & Format(Now, "dd-mmm-yy")

   

   'Create the PDF
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=TempFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=OpenPDFAfterCreating

End Sub
 
If you make the changes in red it compiles and at least does something.

Code:
Sub saveaspdf()

Dim TempFileName As String
[COLOR="Red"]Dim DestFolder As String
Dim wb1 As Excel.Workbook
[/COLOR]
'Prompt for file destination
    With Application.FileDialog(msoFileDialogFolderPicker)
        
        If .Show = True Then
        
            DestFolder = .SelectedItems(1)
            
        Else
        
            MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
                
            Exit Sub
            
        End If
        
    End With

Set wb1 = ActiveWorkbook

TempFileName = Replace(wb1.Name, ".xlsm", "") & " #" & Range("H5") & " " & Format(Now, "dd-mmm-yy")

   

   'Create the PDF
    [COLOR="red"]wb1.[/COLOR]ExportAsFixedFormat Type:=xlTypePDF, Filename:=TempFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=[COLOR="red"]True[/COLOR]

End Sub
 
Great solution. I will post the working code tomorrow for others to benefit from as well. Again, thank you so much.
 

Users who are viewing this thread

Back
Top Bottom