VBA Code - Check and Create Directory

Spaztic

New Member
Joined
Jul 27, 2023
Messages
48
Office Version
  1. 365
Platform
  1. Windows
I need some coding help. The code takes the Excel file and saves as a PDF file with a slightly different file name. The PDF file is supposed to go into a folder called "SavedWork" on the desktop.

Right now, if my desktop has the "SavedWork" folder, the code works (file is put in that folder). But if there isn't the "SavedWork" folder on the desktop, it doesn't (file won't save anywhere / code has error).

How do I change my code to:
* create the "SavedWork" folder on the desktop if it isn't already there on the desktop


VBA Code:
Dim sPath As String, sName As String
desktopDir = CreateObject("WScript.Shell").specialfolders("Desktop")
sPath = desktopDir & "\SavedWork"

sName = ThisWorkbook.Name
sName = Left(sName, InStrRev(sName, ".") - 1)

' Check to see if file exists
    Dim strFileExists As String
    Dim strFileName As String
    Dim tsfile As String
    
    strFileName = sName & "_" & "ISS" & ".pdf"
    tsfile = sPath & "\" & strFileName
    strFileExists = Dir(tsfile)
    
    Dim AnswerYes As String
        
    If strFileExists <> "" Then
        MsgBox strFileName & " already exists", vbExclamation, "FileCheck"
        AnswerYes = MsgBox("Overwrite File?", vbQuestion + vbYesNo, "FileCheck")
        If AnswerYes = vbNo Then
            GoTo Unhide
        End If
    End If
  
ThisWorkbook.Save
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, fileName:=sPath & "\" & sName & "_" & "ISS" & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
There are lots and lots of code examples that creates file folders if they don't exist. Did you try looking at some of them? Some use FSO (file scripting object); some use Shell. Which you want to use might be better for you to search out and see which you find attractive. FSO will require a reference to that library - don't know if that will be an issue for anyone. Here's a few


 
Upvote 0
I'm confused by the "Z1", "Y1", "D1" values. And where do I enter what you have in my code? (Again, I'm pretty new to coding)
 
Upvote 0
Don't know what happened there, that last one was supposed to be a link yet it got posted as code. If you read the thread you'd know what relevance those had but I guess you couldn't! Those cells contain folder/file path info:

EDIT - I can't seem to post only the link, but if you click on the panel right below where that post is "quoted" it will take you there.
 
Upvote 0
This worked... I added the following to the code:
CreateDir (sPath)
-----------------------------------------------------------
Private Sub CreateDir(localDir As String)
On Error GoTo ErrorHandler
MkDir localDir
ErrorHandler:
Exit Sub
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,130
Members
453,021
Latest member
Justyna P

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