Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2016, 08:10 PM
gmaxey gmaxey is offline Define New Valid Folder Name Windows 7 32bit Define New Valid Folder Name Office 2010 (Version 14.0)
Expert
Define New Valid Folder Name
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default Define New Valid Folder Name

Cross posted in VBA Express:
http://www.vbaexpress.com/forum/show...335#post336335

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 at http://www.gregmaxey.com/
Reply With Quote
  #2  
Old 01-11-2016, 10:56 PM
gmayor's Avatar
gmayor gmayor is offline Define New Valid Folder Name Windows 10 Define New Valid Folder Name Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

On what basis is the foldername being created? Is it just a random sequence of letters or letters and numbers, or is there some method behind it?

The FolderExists function on my web site - http://www.gmayor.com/useful_vba_functions.htm will check if the folder name exists, and can be used to quit if the folder name exists. You can adapt the FileNameUnique function to work with Foldernames to append a number if the name exists and the CleanFilename function to vet a name for illegal filename characters, but really it is the original naming that is the main issue.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-12-2016, 04:43 AM
gmaxey gmaxey is offline Define New Valid Folder Name Windows 7 32bit Define New Valid Folder Name Office 2010 (Version 14.0)
Expert
Define New Valid Folder Name
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,428
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Who is this mysterious respondent

I already have the basic FolderExists function. If not mistaken it is a part of our collaborative modAddInUtils module. I'll look again at FileNamUnique.

My first stab is to let the user create or "attempt" to create any folder name they like as long as it doesn't already exists and is a valid folder name.

I think I will add a key press event to keep them from entering invalid characters though.

Thanks!
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Define New Valid Folder Name Exporting .doc as .pdf Not A Valid File Name vbAMEN Word VBA 3 06-09-2014 04:09 PM
how to download and install office 10 with a valid product key ozarkopa Office 6 12-28-2013 06:56 PM
Define New Valid Folder Name How to define restrictions to resource combination andrebmsilva Project 6 11-10-2013 03:02 PM
Office 2008 for Mac - product key not valid NJDevil Office 2 06-21-2010 10:22 AM
Define New Valid Folder Name Powerpoint not recognising wmv as valid movie type Robairto PowerPoint 2 05-20-2010 02:43 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 10:06 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft