+ Reply to Thread
Results 1 to 5 of 5

Create a Folder if it doesn't not exist.

Hybrid View

  1. #1
    Registered User
    Join Date
    08-29-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    5

    Question Create a Folder if it doesn't not exist.

    Hello everyone
    I would like to save a pdf file by taking the information from cell B1 and saving it to its own folder.
    This is the macro that I have created to do this.

    Sub MakePdf()
    '
    ' MakePdf Macro
    '
    ChDir "D:\MyDir\Sub_Dir"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "D:\MyDir\Sub_Dir\File_" & Range("B1").Value & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    End Sub

    A B
    1 Sub_Dir File_Name

    What I don’t know is how to check the information from cell A1 and verify if the folder already exists with that name (Info from A1)
    If it the folder does exist save the file in it, but if it doesn’t then create a new folder and save the file in there.


    Thanks in advance.

    Luis

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Create a Folder if it doesn't not exist.

    Welcome to the forum! Please paste code between code tags. Insert them by clicking the # icon on the reply/post's toolbar.
    ChDir() is seldom needed.

    Sub Main()
      Dim fn As String
      fn = "D:\MyDir\Sub_Dir\"
      CreateObject("WScript.Shell").Exec ("cmd /c md " _
        & """" & fn & """")
    End Sub

  3. #3
    Registered User
    Join Date
    08-29-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    5

    Re: Create a Folder if it doesn't not exist.

    Test to create a folder.png
    Thanks for you help
    Could you please insert for me your recommendation in the macro below , I have very limited knowledge in creating macros

    Sub MakePdf()
    '
    ' MakePdf Macro
    '
    ChDir "C:\MyDir\Sub_Dir"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "C:\MyDir\Sub_Dir\File_" & Range("B1").Value & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    End Sub


    Thanking you in advance.
    Luis

  4. #4
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,693

    Re: Create a Folder if it doesn't not exist.

    @Lucholac44
    Please do not ignore Mr Kenneth Hobson's request regarding code tags.

  5. #5
    Registered User
    Join Date
    08-29-2016
    Location
    Montreal
    MS-Off Ver
    2013
    Posts
    5

    Re: Create a Folder if it doesn't not exist.

    Sub MakePdf()
    '
    ' MakePdf Macro
    '
    ChDir "D:\MyDir\Sub_Dir"
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    "D:\MyDir\Sub_Dir\File_" & Range("B1").Value & ".pdf", Quality:= _
    xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
    OpenAfterPublish:=True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA to save a workbook to a folder but create that folder if it doesn't already exist
    By cnixon in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-24-2014, 06:01 PM
  2. Code to create New Folder (if doesn't exist) and then Save Workbook to folder
    By jenhawley in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-10-2013, 03:09 PM
  3. [SOLVED] Look for a File and create one if it doesn't exist
    By Titansmasher in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-08-2012, 07:43 PM
  4. Replies: 13
    Last Post: 06-04-2012, 02:18 PM
  5. Save a file to a folder, create folder if it doesn't exist
    By mcinnes01 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2010, 09:12 AM
  6. Create a new Sheet if it doesn't exist?
    By nbaj2k in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-28-2006, 06:49 PM
  7. Replies: 6
    Last Post: 08-11-2006, 03:41 PM
  8. [SOLVED] Create Folder If It Doesn't Exist
    By Neutron1871 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-05-2005, 09:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1