MkDir command doesn't work 2nd time

S

Steve C

I am creating a Word 2003 template with a userform where multiple product
specifications can be selected from a list box. Elsewhere in the form, the
user also chooses what project the specifications are for, along with what
department (Engineering, Plumbing, etc., for example) is requesting them.
Upon clicking OK, an existing Word document containing those specifications
is found, opened, updated, then saved to that project’s Spec subfolder in a
subfolder of Spec based on the department. As an example, if the spec is
called Steel Reinforcement, the project is ABC Waterpark (which has a folder
on our P: drive) and the requesting department is Engineering, clicking OK
will save the resulting Word document to this location:

P:\ABC Waterpark\Specs\Engineering\Steel Reinforcement.doc.

I’ve written most of the code to do this. However, here’s my glitch: For
the several hundred projects on our P: drive, most do not have a Specs
subfolder (and consequently, no departmental subfolder(s) like Engineering
either). I’m trying to use the MkDir command to create them when necessary.

If there is no Specs subfolder, my MkDir code below successfully creates it.
But if an Engineering (or any other department) subfolder needs creating
after the Spec folder is, I get a run-time error 4172 Path Not Found. Funny
thing is, if I go into the Immediate Window and run the MkDir command, it
works! Any thoughts on why the second MkDir command results in an error?
I'm sure my code can be vastly improved. Thanks for any suggestions you can
give me!

Sub SaveSelectedSpec (Path, SpecName, RequestingDept)

' Called by: Private Sub cmdOK_Click
' Purpose: Saves all documents selected in opening form to a departmental
' subfolder within the desired P: drive project’s Specs subfolder. If Specs
does not
‘ exist, create it first, then also create the appropriate departmental
subfolder of
' Specs to place the selected Word Document in.

‘ Coming into this procedure, the above variable values are as follows:
‘ Path = “20080050 – ABC Waterpark†(all projects have a folder on our P:
drive)
‘ SpecName = “Steel Reinforcement.docâ€
‘ RequestingDept = “Engineeringâ€


On Error GoTo NoSpecsFolder

‘ if Specs folder doesn’t exist, error will execute code to create one
' (this is working as desired)

ChangeFileOpenDirectory "P:\" & Path & "\Specs\ "
GoTo CreateSubDept ‘ if Specs folder exists, then create the department
subfolder

NoSpecsFolder:
'Creates a new Specs folder if none currently exists
MkDir "P:\" & Path & "\Specs\" ‘ again, this command works as desired

CreateSubDept:

On Error GoTo NoDeptFolder ‘ if the Requesting Dept. subfolder does not
exist,
' error will execute code to create one

ChangeFileOpenDirectory "P:\" & Path & "\Specs\" & RequestingDept & "\"
‘this is
' where the run-time error 4172 Path Not Found pops up, rather than going to
the
' NoDeptFolder command line. At this point, I can paste the MkDir command
below
' into the Immediate Window, and it works! But why not here???

GoTo SaveDoc ‘ if dept. folder exists, then save document there

NoDeptFolder: 'Creates a new Dept subfolder withing Specs if none exists
MkDir "P:\" & Path & "\Specs\Specs\" & RequestingDept & "\"

SaveDoc:
ActiveDocument.SaveAs FileName:= _
SpecName, FileFormat:= _
wdFormatDocument, LockComments:=False, Password:="",
AddToRecentFiles:= _
True, WritePassword:="", ReadOnlyRecommended:=False,
EmbedTrueTypeFonts:= _
False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
SaveAsAOCELetter:=False
Exit Sub

End Sub
 
D

Doug Robbins - Word MVP

In your code, you are trying to change the directory to a "RequestingDept"
sub folder of the Specs folder, whether or not, the Specs folder existed.
You need to change your code so that instead of creating just the Specs
folder, it also creates the RequestingDept subfolder if the Specs folder
does not exist

NoSpecsFolder:
'Creates a new Specs folder if none currently exists
MkDir "P:\" & Path & "\Specs\"
MkDir "P:\" & Path & "\Specs\" & RequestingDept & "\"

You probably should also be checking Error 76 and create the folders only if
that error occurs rather than doing it on any error.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top