VBA Export As PDF & Create Folder If Does Not Exist

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hey,

So I managed to create the following code to save a folder using 4 different variables for the file path and file name.

But firstly I need to check if a folder/filepath exists, if not to create one e.g. C:\Users\Tiana\Documents\Branch Merchandising\SS17\Branch Stock Report\Week 49.. so check if week 49 exists, if it does carry on, if not create a week 49 folder.. or week 50 etc etc. and then once it's saved to then export Sheet(1) as PDF.

Greatly appreciate all the help :) even if it's to tidy my existing code up!

Thanks,

Code:
Sub SaveAsAndExport()

Dim FPath As String
Dim Season As Range, Yr As Range, Wk As Range, Day As Range


Set Season = ActiveWorkbook.ActiveSheet.Range("M1")
Set Yr = ActiveWorkbook.ActiveSheet.Range("M2")
Set Wk = ActiveWorkbook.ActiveSheet.Range("M3")
Set Day = ActiveWorkbook.ActiveSheet.Range("M4")


Application.DisplayAlerts = False


FPath = "C:\Users\xxxxx\Documents\Branch Merchandising\" & Season & Yr & _
        "\Branch Stock Report\Week " & Wk & "\"


ActiveWorkbook.SaveAs FPath & Season & Yr & " " & "Week " & Wk & " " & Day & " - Branch Stock Report" & ".xlsm"


End Sub
 
Last edited:

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Okay so I've figured out how to save it as pdf :)

Just need help on checking if a folder exists, if not create using "Week " & wk..

Code:
Sub SaveAs()

Dim FPath As String, FName As String
Dim Season As Range, Yr As Range, Wk As Range, Day As Range


Set Season = ActiveWorkbook.Sheets(2).Range("M1")
Set Yr = ActiveWorkbook.Sheets(2).Range("M2")
Set Wk = ActiveWorkbook.Sheets(2).Range("M3")
Set Day = ActiveWorkbook.Sheets(2).Range("M4")


Application.DisplayAlerts = False


FPath = Environ("Userprofile") & "\Documents\Branch Merchandising\" & Season & Yr & _
        "\Branch Stock Report\Week " & Wk & "\"


FName = Season & Yr & " Week " & Wk & " " & Day & " - Branch Stock Report"


ActiveWorkbook.SaveAs FPath & FName & ".xlsm"


ThisWorkbook.Sheets(1).ExportAsFixedFormat Type:=xlTypePDF, Filename:=FPath & FName & ".pdf" _
    , openafterpublish:=True
    
MsgBox FName & " was saved and exported to PDF."


End Sub
 
Upvote 0
This checks if the FPath folder exists and creates it if not:
Code:
    If Dir(FPath, vbDirectory) = vbNullString Then
        MkDir FPath
        MsgBox "Created folder " & FPath
    End If
 
Upvote 0
This checks if the FPath folder exists and creates it if not:
Code:
    If Dir(FPath, vbDirectory) = vbNullString Then
        MkDir FPath
        MsgBox "Created folder " & FPath
    End If

Thanks John_w really helpful. I found out that it needs to look at the last folder to create one and as I'm using many variables which would all need to create folders I've constructed 3 different ifs, how can I nest these together?

Code:
    'Create Season folder if not existIf Len(Dir(PPath, vbDirectory)) = 0 Then
    MkDir (PPath)
End If


    'Create Branch Stock Report folder if not exist
If Len(Dir(BSR, vbDirectory)) = 0 Then
    MkDir (BSR)
End If


    'Create Week folder if not exist
If Len(Dir(FPath, vbDirectory)) = 0 Then
    MkDir (FPath)
End If
 
Upvote 0
You shouldn't need to nest the Ifs: just check the folders in sequence as that code is doing.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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