Consulting

Results 1 to 11 of 11

Thread: Does a path exist?

  1. #1
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    Does a path exist?

    My problem is that if the folder doesn't exist, I get the "Error Msg". But what I'm
    trying to figure out is how to bypass the error msg by popping a "Find the
    correct correct path or a create the path box. Any comments?



    ActiveWorkbook.SaveAs FileName:= _
    "S:\Daily Production Press Efficiency\" & "EFFICIENCY REPORTS " & _
    NumYear & "\" & NumMonth & "-" & Right(NumYear, 2) & _
    " PRODUCTION EFFICIENCY REPORT.xls", FileFormat:= _
    xlNormal, & _
    Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
    , CreateBackup:=False

  2. #2
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hi Daxton!

    From a forthcoming KB entry (in approval process right now)

    Function FileOrDirExists(PathName As String) As Boolean
    'Macro Purpose: Function returns TRUE if the specified file
    '               or directory exists, false if not.
    'File usage   : Provide full file path and extension
    'Path usage   : Provide full file path only (accepts with/without trailing "\")
    Dim sTemp As String
    'Ignore errors to allow for error evaluation
        On Error Resume Next
        sTemp = GetAttr(PathName)
    'Check if error exists and set response appropriately
        Select Case Err.Number
            Case Is = 0
                FileOrDirExists = True
            Case Else
                FileOrDirExists = False
        End Select
    'Resume error checking
        On Error GoTo 0
    End Function
    So you could use it like so:

    Sub TestIt()
    'Macro Purpose: To test the FileOrDirExists function
    'Only included to demonstrate the function.  NOT required for normal use!
    Dim sPath As String
    'Change your directory here
    sPath = 
    "S:\Daily Production Press Efficiency\" & "EFFICIENCY REPORTS " & _
    NumYear & "\"  
    'Test if directory or file exists
    If FileOrDirExists(sPath) Then
    ActiveWorkbook.SaveAs FileName:= _ 
    spath & NumMonth & "-" & Right(NumYear, 2) & _
     " PRODUCTION EFFICIENCY REPORT.xls", FileFormat:= _ 
    xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _ 
    , CreateBackup:=False 
    Else
        MsgBox sPath & " does not exist."
    End If
    End Sub
    HTH,
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Daxton A.
    My problem is that if the folder doesn't exist, I get the "Error Msg". But what I'm
    trying to figure out is how to bypass the error msg by popping a "Find the
    correct correct path or a create the path box. Any comments?



    ActiveWorkbook.SaveAs FileName:= _ 
    "S:\Daily Production Press Efficiency\" & "EFFICIENCY REPORTS " & _ 
    NumYear & "\" & NumMonth & "-" & Right(NumYear, 2) & _ 
    " PRODUCTION EFFICIENCY REPORT.xls", FileFormat:= _ 
    xlNormal, & _ 
    Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _ 
    , CreateBackup:=False
    Create it.


    Dim sFolder As String
    Dim sFile As String
        sFolder = "S:\Daily Production Press Efficiency\" & "EFFICIENCY REPORTS " & NumYear
        sFile = NumMonth & "-" & Right(NumYear, 2) & " PRODUCTION EFFICIENCY REPORT.xls"
        On Error Resume Next
            MkDir sFolder
        On Error GoTo 0
        ActiveWorkbook.SaveAs Filename:=sFolder & "\" & sFile, _
                              FileFormat:=xlNormal, _
                              Password:="", _
                              WriteResPassword:="", _
                              ReadOnlyRecommended:=False, _
                              CreateBackup:=False
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Contributor Daxton A.'s Avatar
    Joined
    Jun 2004
    Location
    Biloxi, Mississippi
    Posts
    143
    Location

    Thanking everyone for all the help...

    This message has been solved

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kpuls

    Dim sTemp As String
    'Ignore errors to allow for error evaluation
        On Error Resume Next
        sTemp = GetAttr(PathName)
    Ken,

    GetAttr returns an integer, not a string.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    GetAttr returns an integer, not a string.
    Well, now that is a curious thing... the method works anyway. I'm going to have to play with that one a bit.

    Thanks!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  7. #7
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Hello,

    I ran through it, and it works because you can read an integer into a string as text. It caught me a little off guard, only because I'm used to seeing a Type Mismatch error when you use the wrong data type.

    Because the function only tests if it was able, and doesn't actually use the value, it actually doesn't really affect it much in this case. Ideally, though, sTemp should actually be defined as an Integer (and probably with a name like iTemp instead.)

    Thanks for pointing it out, xld!
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by kpuls
    I ran through it, and it works because you can read an integer into a string as text. It caught me a little off guard, only because I'm used to seeing a Type Mismatch error when you use the wrong data type.

    Because the function only tests if it was able, and doesn't actually use the value, it actually doesn't really affect it much in this case. Ideally, though, sTemp should actually be defined as an Integer (and probably with a name like iTemp instead.
    Yes, I am not surprised it doesn't complain, as it would seem that VBA casts it into a string, because it can (unlike trying to cast "ABC" into a number). I pointed it out as because it does work doesn't mean that you should use it (unless you are doing so to get some undocumented advantage) as it may not work in a later version where rules may be more rigidly enforced.

    And,it becomes more relevant when using GetAttr for what it was intended for, to get file attrributes such as hidden flags.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Quote Originally Posted by xld
    Yes, I am not surprised it doesn't complain, as it would seem that VBA casts it into a string, because it can (unlike trying to cast "ABC" into a number). I pointed it out as because it does work doesn't mean that you should use it (unless you are doing so to get some undocumented advantage) as it may not work in a later version where rules may be more rigidly enforced.

    And,it becomes more relevant when using GetAttr for what it was intended for, to get file attrributes such as hidden flags.
    Absolutely agree with you on all points.

    It will be fixed in the KB entry.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





  10. #10
    Moderator VBAX Mentor sheeeng's Avatar
    Joined
    May 2005
    Location
    Kuala Lumpur
    Posts
    392
    Location
    Quote Originally Posted by kpuls
    Absolutely agree with you on all points.

    It will be fixed in the KB entry.
    Great...
    Keep it up..

  11. #11
    Moderator VBAX Guru Ken Puls's Avatar
    Joined
    Aug 2004
    Location
    Nanaimo, BC, Canada
    Posts
    4,001
    Location
    Just for reference... Check if directory or file exists is now updated in the KB.
    Ken Puls, CMA - Microsoft MVP (Excel)
    I hate it when my computer does what I tell it to, and not what I want it to.

    Learn how to use our KB tags! -||- Ken's Excel Website -||- Ken's Excel Forums -||- My Blog -||- Excel Training Calendar

    This is a shameless plug for my new book "RibbonX - Customizing the Office 2007 Ribbon". Find out more about it here!

    Help keep VBAX clean! Use the 'Thread Tools' menu to mark your own threads solved!





Posting Permissions

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