Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

Creating a new file folder

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roger - 20 Jun 2005 15:41 GMT
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 - 20 Jun 2005 18:02 GMT
> 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,
[quoted text clipped - 28 lines]
> 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

Signature

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

Roger - 18 Jul 2005 17:59 GMT
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

> > 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,
[quoted text clipped - 49 lines]
>       FSO.CreateFolder NewDir
>    End If
Jay Freedman - 18 Jul 2005 18:12 GMT
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.

Signature

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

> When I try this code I get an error message stating:
> "user defined type not defined" with the Dim statement:
[quoted text clipped - 63 lines]
>> Jay Freedman
>> Microsoft Word MVP          FAQ: http://word.mvps.org
Roger - 18 Jul 2005 18:35 GMT
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

> Sorry, I was not quite right when I said this:
>
[quoted text clipped - 70 lines]
> >> Jay Freedman
> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Roger - 19 Jul 2005 18:36 GMT
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:
>
[quoted text clipped - 70 lines]
> >> Jay Freedman
> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Jay Freedman - 20 Jul 2005 03:33 GMT
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

>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
[quoted text clipped - 82 lines]
>> >> Jay Freedman
>> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Roger - 20 Jul 2005 20:48 GMT
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"
[quoted text clipped - 123 lines]
> >> >> Jay Freedman
> >> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Jay Freedman - 21 Jul 2005 01:54 GMT
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

>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
[quoted text clipped - 143 lines]
>> >> >> Jay Freedman
>> >> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Roger - 27 Jul 2005 17:56 GMT
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.

> Hi Roger,
>
[quoted text clipped - 161 lines]
> >> >> >> Jay Freedman
> >> >> >> Microsoft Word MVP          FAQ: http://word.mvps.org
Jay Freedman - 27 Jul 2005 22:17 GMT
Good, I'm glad you were able to get it to work. Thanks for letting me know.

Signature

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

> 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
[quoted text clipped - 179 lines]
>>>>>>>> Jay Freedman
>>>>>>>> Microsoft Word MVP          FAQ: http://word.mvps.org
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.