Creating a new file folder

R

Roger

I would like to add a feature to the following code. I'd like to be able to
have the macro check for the existence of the File Folder, and if the folder
does not exist, to create the folder, and then save the file to that newly
created folder. I have looked in the VBA editor help files and the Word MVP
site for some guidance in how to accomplish this, but haven't found anything
that really helps. Here is the code that I want to add on to:

With ActiveDocument

Dim CaseNumber As String
Dim DocFileName As String
CaseNumber = ActiveDocument.Bookmarks("SaveFileNumber").Range.Text
DocFileName = ActiveDocument.Bookmarks("DocFileName").Range.Text

With Dialogs(wdDialogFileSaveAs)
.Name = "\\lvco-dc\prosecutor\CaseDocs\" & CaseNumber & "\" &
DocFileName
.Show
End With

End With

The name of the file folder that I want to search for is represented by the
variable called CaseNumber. Currently, if the folder for the CaseNumber
variable doesn't exist, then the SaveAs dialog box opens up at the CaseDocs
folder. Instead of having that happen, I would like to create a new folder
in CaseDocs with that new folder's name being supplied by the CaseNumber
variable.

Any assistance would be greatly appreciated.
Roger
 
J

Jay Freedman

Roger said:
I would like to add a feature to the following code. I'd like to be
able to have the macro check for the existence of the File Folder,
and if the folder does not exist, to create the folder, and then save
the file to that newly created folder. I have looked in the VBA
editor help files and the Word MVP site for some guidance in how to
accomplish this, but haven't found anything that really helps. Here
is the code that I want to add on to:

With ActiveDocument

Dim CaseNumber As String
Dim DocFileName As String
CaseNumber = ActiveDocument.Bookmarks("SaveFileNumber").Range.Text
DocFileName = ActiveDocument.Bookmarks("DocFileName").Range.Text

With Dialogs(wdDialogFileSaveAs)
.Name = "\\lvco-dc\prosecutor\CaseDocs\" & CaseNumber & "\" &
DocFileName
.Show
End With

End With

The name of the file folder that I want to search for is represented
by the variable called CaseNumber. Currently, if the folder for the
CaseNumber variable doesn't exist, then the SaveAs dialog box opens
up at the CaseDocs folder. Instead of having that happen, I would
like to create a new folder in CaseDocs with that new folder's name
being supplied by the CaseNumber variable.

Any assistance would be greatly appreciated.
Roger

Hi Roger,

The easiest way to implement this is to use the FileScriptingObject.

It's helpful but not required to first click Tools > References and check
the Microsoft Scripting Runtime item. That gives you IntelliSense support
and access to the Help topics.

Then add something like this to your code before the Dialogs() statement:

Dim FSO As FileSystemObject
Dim NewDir As String

Set FSO = CreateObject("Scripting.FileSystemObject")

NewDir = "\\lvco-dc\prosecutor\CaseDocs\" & CaseNumber

If Not FSO.FolderExists(NewDir) Then
FSO.CreateFolder NewDir
End If
 
R

Roger

When I try this code I get an error message stating:
"user defined type not defined" with the Dim statement:
"FSO As FileSystemObject" highlighted in my code. How do I define my user
defined statement?

Roger
 
J

Jay Freedman

Sorry, I was not quite right when I said this:
It's helpful but not required to first click Tools > References and check
the Microsoft Scripting Runtime item.

It *is* required so the FileSystemObject type will be defined.
 
R

Roger

OK, well I thought I had checked it, but I must have missed that step. It
runs great now. Thank you for the quick response to my slow question.

Roger
 
R

Roger

Is it possible to add into the code below a way for the program to check and
make sure that the value returned by the "CaseNumber" bookmark is in the
proper format?

Now that I've added the auto folder creation capability, I'm concerned what
will happen if a user edits the contents of the bookmark and changes the
value to something that doesn't match the specified standard.

If this is possible, I'd like to see an example of how to accomplish it?
Thanks.
 
J

Jay Freedman

Generally, yes it is possible. If you want me to write it for you,
though, you're going to have to tell me what the "specified standard"
is.

The following example will accept numbers like 4.7 or 99.4 and will
reject everything else. The Like operator is a pattern-matching
function, and the expression that follows it is the pattern.

Dim CaseNumber As String
Dim DocFileName As String
Dim msg As String
msg = "The text you entered for the case number is not " & _
"in the right format. Please enter the correct number."

With ActiveDocument
' first check whether the bookmark exists
' (it could have been deleted)
If .Bookmarks.Exists("SaveFileNumber") Then
CaseNumber = .Bookmarks("SaveFileNumber").Range.Text
End If

' check that the string has the right format
Do While Not ((CaseNumber Like "#.#") Or _
(CaseNumber Like "##.#"))
' if not, ask user for correct value
CaseNumber = InputBox(msg, "Error")
If CaseNumber = "" Then Exit Sub ' canceled
Loop

DocFileName = .Bookmarks("DocFileName").Range.Text
End With

If the correct format is more complicated, the code that validates the
bookmark could be put into a separate function that just returns True
(the value is OK) or False (it doesn't match the requirement).

--
Regards,
Jay Freedman
Microsoft Word MVP

Is it possible to add into the code below a way for the program to check and
make sure that the value returned by the "CaseNumber" bookmark is in the
proper format?

Now that I've added the auto folder creation capability, I'm concerned what
will happen if a user edits the contents of the bookmark and changes the
value to something that doesn't match the specified standard.

If this is possible, I'd like to see an example of how to accomplish it?
Thanks.
 
R

Roger

Jay, what you've suggested is along the lines of what I'd like to do. Just
today, someone made an edit to a document and deleted the bookmark
DocFileName. I knew what had happened, but she was presented with the debug
screen. I can prevent that from happening by tucking the bookmark away in a
header where it's unlikely to be disturbed, but I never thought about
running a check in the code to make sure it existed. That would be better
practice, along with protecting the bookmark.

As for the CaseNumber format it's always ####-AB-######. By that I mean
it's always comprised of "Year" "-" "2 letter CaseType" "-" "6 digit number"
as in 2005-CT-000015 for example. I'm guessing that would require calling a
function to run this validation test. I've never done that before.

Roger
 
J

Jay Freedman

Hi Roger,

Actually, that pattern is easier to check than one that might have a
variable number of characters. In the last macro I posted, replace the
lines
Do While Not ((CaseNumber Like "#.#") Or _
(CaseNumber Like "##.#"))

with this:

Do While Not (CaseNumber Like "####-[A-Z][A-Z]-######")
 
R

Roger

Thank you Jay. That worked out perfect. I was able to add a similar check
for the other bookmark with a message box for input in case the bookmark's
not found.


Jay Freedman said:
Hi Roger,

Actually, that pattern is easier to check than one that might have a
variable number of characters. In the last macro I posted, replace the
lines
Do While Not ((CaseNumber Like "#.#") Or _
(CaseNumber Like "##.#"))

with this:

Do While Not (CaseNumber Like "####-[A-Z][A-Z]-######")

--
Regards,
Jay Freedman
Microsoft Word MVP

Jay, what you've suggested is along the lines of what I'd like to do. Just
today, someone made an edit to a document and deleted the bookmark
DocFileName. I knew what had happened, but she was presented with the debug
screen. I can prevent that from happening by tucking the bookmark away in a
header where it's unlikely to be disturbed, but I never thought about
running a check in the code to make sure it existed. That would be better
practice, along with protecting the bookmark.

As for the CaseNumber format it's always ####-AB-######. By that I mean
it's always comprised of "Year" "-" "2 letter CaseType" "-" "6 digit number"
as in 2005-CT-000015 for example. I'm guessing that would require calling a
function to run this validation test. I've never done that before.

Roger

check
and to
be
 
J

Jay Freedman

Good, I'm glad you were able to get it to work. Thanks for letting me know.

--
Regards,
Jay Freedman
Microsoft Word MVP
Thank you Jay. That worked out perfect. I was able to add a similar
check for the other bookmark with a message box for input in case the
bookmark's not found.


Jay Freedman said:
Hi Roger,

Actually, that pattern is easier to check than one that might have a
variable number of characters. In the last macro I posted, replace
the lines
Do While Not ((CaseNumber Like "#.#") Or _
(CaseNumber Like "##.#"))

with this:

Do While Not (CaseNumber Like "####-[A-Z][A-Z]-######")

--
Regards,
Jay Freedman
Microsoft Word MVP

Jay, what you've suggested is along the lines of what I'd like to
do. Just today, someone made an edit to a document and deleted the
bookmark DocFileName. I knew what had happened, but she was
presented with the debug screen. I can prevent that from happening
by tucking the bookmark away in a header where it's unlikely to be
disturbed, but I never thought about running a check in the code to
make sure it existed. That would be better practice, along with
protecting the bookmark.

As for the CaseNumber format it's always ####-AB-######. By that I
mean it's always comprised of "Year" "-" "2 letter CaseType" "-" "6
digit number" as in 2005-CT-000015 for example. I'm guessing that
would require calling a function to run this validation test. I've
never done that before.

Roger


Generally, yes it is possible. If you want me to write it for you,
though, you're going to have to tell me what the "specified
standard" is.

The following example will accept numbers like 4.7 or 99.4 and will
reject everything else. The Like operator is a pattern-matching
function, and the expression that follows it is the pattern.

Dim CaseNumber As String
Dim DocFileName As String
Dim msg As String
msg = "The text you entered for the case number is not " & _
"in the right format. Please enter the correct number."

With ActiveDocument
' first check whether the bookmark exists
' (it could have been deleted)
If .Bookmarks.Exists("SaveFileNumber") Then
CaseNumber = .Bookmarks("SaveFileNumber").Range.Text
End If

' check that the string has the right format
Do While Not ((CaseNumber Like "#.#") Or _
(CaseNumber Like "##.#"))
' if not, ask user for correct value
CaseNumber = InputBox(msg, "Error")
If CaseNumber = "" Then Exit Sub ' canceled
Loop

DocFileName = .Bookmarks("DocFileName").Range.Text
End With

If the correct format is more complicated, the code that validates
the bookmark could be put into a separate function that just
returns True (the value is OK) or False (it doesn't match the
requirement).

--
Regards,
Jay Freedman
Microsoft Word MVP

Is it possible to add into the code below a way for the program to check
and
make sure that the value returned by the "CaseNumber" bookmark is
in the proper format?

Now that I've added the auto folder creation capability, I'm
concerned what will happen if a user edits the contents of the
bookmark and changes the value to something that doesn't match
the specified standard.

If this is possible, I'd like to see an example of how to
accomplish it? Thanks.

Sorry, I was not quite right when I said this:

It's helpful but not required to first click Tools > References and
check
the Microsoft Scripting Runtime item.

It *is* required so the FileSystemObject type will be defined.

--
Regards,
Jay Freedman
Microsoft Word MVP

Roger wrote:
When I try this code I get an error message stating:
"user defined type not defined" with the Dim statement:
"FSO As FileSystemObject" highlighted in my code. How do I
define my user defined statement?

Roger

Roger wrote:
I would like to add a feature to the following code. I'd like to
be
able to have the macro check for the existence of the File
Folder, and if the folder does not exist, to create the
folder, and then save the file to that newly created folder.
I have looked in the VBA editor help files and the Word MVP
site for some guidance in how to accomplish this, but haven't
found anything that really helps. Here is the code that I
want to add on to:

With ActiveDocument

Dim CaseNumber As String
Dim DocFileName As String
CaseNumber =
ActiveDocument.Bookmarks("SaveFileNumber").Range.Text
DocFileName =
ActiveDocument.Bookmarks("DocFileName").Range.Text

With Dialogs(wdDialogFileSaveAs)
.Name = "\\lvco-dc\prosecutor\CaseDocs\" & CaseNumber &
"\" & DocFileName
.Show
End With

End With

The name of the file folder that I want to search for is
represented by the variable called CaseNumber. Currently, if
the folder for the CaseNumber variable doesn't exist, then
the SaveAs dialog box opens up at the CaseDocs folder.
Instead of having that happen, I would like to create a new
folder in CaseDocs with that new folder's name being supplied
by the CaseNumber variable.

Any assistance would be greatly appreciated.
Roger

Hi Roger,

The easiest way to implement this is to use the
FileScriptingObject.

It's helpful but not required to first click Tools >
References and check the Microsoft Scripting Runtime item.
That gives you IntelliSense support and access to the Help
topics.

Then add something like this to your code before the Dialogs()
statement:

Dim FSO As FileSystemObject
Dim NewDir As String

Set FSO = CreateObject("Scripting.FileSystemObject")

NewDir = "\\lvco-dc\prosecutor\CaseDocs\" & CaseNumber

If Not FSO.FolderExists(NewDir) Then
FSO.CreateFolder NewDir
End If
 

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