Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating a new file folder

2 views
Skip to first unread message

Roger

unread,
Jun 20, 2005, 10:41:31 AM6/20/05
to
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


Jay Freedman

unread,
Jun 20, 2005, 1:02:41 PM6/20/05
to

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

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org


Roger

unread,
Jul 18, 2005, 12:59:54 PM7/18/05
to
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

"Jay Freedman" <jay.fr...@verizon.net> wrote in message
news:OoHGgnbd...@TK2MSFTNGP09.phx.gbl...

Jay Freedman

unread,
Jul 18, 2005, 1:12:26 PM7/18/05
to
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 FAQ: http://word.mvps.org

Roger

unread,
Jul 18, 2005, 1:35:43 PM7/18/05
to
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

"Jay Freedman" <jay.fr...@verizon.net> wrote in message

news:uEbmlv7i...@tk2msftngp13.phx.gbl...

Roger

unread,
Jul 19, 2005, 1:36:38 PM7/19/05
to
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.

"Jay Freedman" <jay.fr...@verizon.net> wrote in message

news:uEbmlv7i...@tk2msftngp13.phx.gbl...

Jay Freedman

unread,
Jul 19, 2005, 10:33:27 PM7/19/05
to

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 FAQ: http://word.mvps.org

Roger

unread,
Jul 20, 2005, 3:48:52 PM7/20/05
to
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

"Jay Freedman" <jay.fr...@verizon.net> wrote in message

news:0mdrd1td6920vl68j...@4ax.com...

Jay Freedman

unread,
Jul 20, 2005, 8:54:45 PM7/20/05
to
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 FAQ: http://word.mvps.org

Roger

unread,
Jul 27, 2005, 12:56:43 PM7/27/05
to
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" <jay.fr...@verizon.net> wrote in message

news:2dstd1t82fmjaetsu...@4ax.com...

Jay Freedman

unread,
Jul 27, 2005, 5:17:14 PM7/27/05
to
Good, I'm glad you were able to get it to work. Thanks for letting me know.
0 new messages