hi,
Sorry, I'm being a bit lazy so this is untested too - hopefully it works ;-)
(see between the stars)
I have used the same type of approach as Richard but I used the other function "doesfilefolderexist" which returns a boolean response rather than the "getactualfoldername" which gives a folder name. btw, I don't know much about fso objects either - this was really a stolen & modified function.
Also, I've inserted a second check in case the "Ordering Docs" folder doesn't exist either.
Option Explicit
Sub SaveJobFile()
'declare all variables
Dim stOfPath As String
Dim ActualMidFolder As String
Dim EndOfNameAndPath As String
Dim FileNameToSave As String
Dim JobNum As String
Dim PONum As String
Dim SuppliersName As String
Dim ChosenFolder As String
'define initial variables
JobNum = Range("G23").Value
PONum = Range("k12").Value
SuppliersName = Range("b16")
EndOfNameAndPath = SuppliersName & " Purchase Order " & PONum & Format(Date, " dd.mm.yy") & ".xls"
ChosenFolder = "Docs" 'default value
'test to see if it is stock or PO (?) & define the saving path accordingly
Select Case UCase(Left(JobNum, 1))
Case Is = "J"
'Rob's test path:
stOfPath = "C:\Documents and Settings\HP_Owner\My Documents\"
' stOfPath = Range("B69") & "\"
'test for any likely folders
If Not (DoesFileFolderExist(stOfPath & JobNum & "*")) Then GoTo TheEnd
'identify the exact folder
ActualMidFolder = GetActualFolderName(stOfPath, JobNum) & "\"
'**************************************
'Test for existence of "Docs" folder & then for existence of the alternative folder (if needed)
If Not (DoesFileFolderExist(stOfPath & ActualMidFolder & ChosenFolder)) Then
ChosenFolder = "Ordering Docs"
If Not (DoesFileFolderExist(stOfPath & ActualMidFolder & ChosenFolder)) Then GoTo TheEnd
End If
'**************************************
'define rest of file name
EndOfNameAndPath = ChosenFolder & "\" & EndOfNameAndPath
FileNameToSave = stOfPath & ActualMidFolder & EndOfNameAndPath
Case Else
stOfPath = Range("B70") & "\"
If Not (DoesFileFolderExist(stOfPath)) Then GoTo TheEnd
FileNameToSave = stOfPath & EndOfNameAndPath
End Select
'save the file & finish macro
ActiveWorkbook.SaveAs Filename:=FileNameToSave
Exit Sub
TheEnd:
'warn nothing exists & end the macro
MsgBox "Macro ending b/c either:" & Chr(13) & _
"1) no Folder with the following Job number exists : " & stOfPath & JobNum & "*", , "FYI" & Chr(13) & _
"or" & Chr(13) & "2) the subfolder to save into does not exist: " & stOfPath & ActualMidFolder & ChosenFolder
Debug.Print stOfPath & JobNum & "*"
End Sub
Public Function DoesFileFolderExist(strfullpath As String) As Boolean
'sourced from www.excelguru.ca/node/30 by Ken Puls
'note it only checks for the existence of the lowest folder (or the file) in the strfullpath string.
If Not Dir(strfullpath, vbDirectory) = vbNullString Then DoesFileFolderExist = True
End Function
Function GetActualFolderName(StartOfPath As String, StartOfFuzzyFolder As String)
'sourced & modified from http://www.themssforum.com/ExcelProgramming/parent-folder/
'this loops through all subfolders in a named folder & gets the full name of _
the subfolder that starts with the "job number" (existence has been _
previously confirmed but w/o explicitly naming the folder. There may be _
a quicker/tidier way of doing this - but I haven't found it yet...
Dim oFSO As Object
Dim SubDirectory
Set oFSO = CreateObject("Scripting.FileSystemObject")
With oFSO.GetFolder(StartOfPath)
For Each SubDirectory In .SubFolders
If UCase(Left(SubDirectory.Name, Len(StartOfFuzzyFolder))) = UCase(StartOfFuzzyFolder) Then
GetActualFolderName = SubDirectory.Name
Exit For
End If
Next SubDirectory
End With
'free memory
Set oFSO = Nothing
End Function
hth
Rob
Bookmarks