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
--
Steve C

Re: MkDir command doesn't work 2nd time by Doug

Doug
Wed Jul 16 14:59:03 PDT 2008

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

"Steve C" <SteveC@discussions.microsoft.com> wrote in message
news:1550D22B-CD8F-405F-92B4-8D8389F28C0A@microsoft.com...
>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
> --
> Steve C



Re: MkDir command doesn't work 2nd time by SteveC

SteveC
Thu Jul 17 06:05:02 PDT 2008

Doug,

Thanks so much. Simple logic, but it did the trick!
--
Steve C


"Doug Robbins - Word MVP" wrote:

> 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
>
> "Steve C" <SteveC@discussions.microsoft.com> wrote in message
> news:1550D22B-CD8F-405F-92B4-8D8389F28C0A@microsoft.com...
> >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
> > --
> > Steve C
>
>
>