Consulting

Results 1 to 3 of 3

Thread: Define New Valid Folder Name

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,411
    Location

    Define New Valid Folder Name

    Cross posted in Microsoft Office forums:
    http://www.msofficeforums.com/word-v...lder-name.html

    I'm working on a project where the user is required to identify a new unique folder name prior to further processing.

    If the folder identified already exists processing cannot continue. If the folder identified is invalid (invalid characters or reserved) then processing cannot continue.

    The only thing I can think of is:
    1) Check if folder exists and
    2) Try to create the folder and if it fails ...

    I created the following function. Anyone have a better idea?


    Code:
    Sub Test()
      MsgBox fcnIsNewValidFolderName("Test") 'New valid folder name.
      MsgBox fcnIsNewValidFolderName("My Documents") 'Existing folder - returns false
      MsgBox fcnIsNewValidFolderName("A*B?C") 'Invalid characters in name - returns false
      MsgBox fcnIsNewValidFolderName("PRN") 'Reserved name - returns false
    lbl_Exit:
      Exit Sub
    End Sub
    Function fcnIsNewValidFolderName(strFolder As String) As Boolean
    Dim oFSO As Object, oRootFolder As Object, oFolder As Object
      fcnIsNewValidFolderName = True
      Set oFSO = CreateObject("Scripting.FileSystemObject")
      On Error GoTo Err_Root
      Set oRootFolder = oFSO.GetFolder("D:\")
      On Error GoTo Err_Create
      Set oFolder = oRootFolder.SubFolders(strFolder)
      fcnIsNewValidFolderName = False
      GoTo lbl_Exit
    CreateReEntry:
      On Error GoTo Err_Last
      'See if a folder can be created using the folder name passed.
      Set oFolder = oFSO.CreateFolder(oRootFolder & Application.PathSeparator & strFolder)
      oFolder.Delete
    lbl_Exit:
      Set oFSO = Nothing
      Exit Function
    Err_Root:
      fcnIsNewValidFolderName = True
      Resume lbl_Exit
    Err_Create:
      Resume CreateReEntry:
    Err_Last:
      Beep
      fcnIsNewValidFolderName = False
      Resume lbl_Exit
    End Function
    __________________
    Greg Maxey
    Please visit my web site athttp://gregmaxey.mvps.org
    Last edited by SamT; 01-12-2016 at 02:37 PM. Reason: Added CODE Tags with # Icon. LOL
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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