Results 1 to 13 of 13
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    VB? to Create Folders (Office XP)

    I'm really shooting from the hip here. Hope there's a VB/VBA solution to this. Apparently, in WordPerfect you can create a macro that will prompt the user for a file name, it will then create a folder in a "root" directory using this name, and then create a set of other subfolders within this master folder.

    The client is moving out of WordPerfect to Word and, naturally, cannot duplicate this process in Word. Is there a VB solution to this? Phew!

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB? to Create Folders (Office XP)

    Look at the MkDir command to make a folder.

    To prompt the user, you have simple (no validation) choices --
    InputBox, userform textbox control on a custom form -- and a bit
    more sophisticated approach using a Windows API function
    which is not as intimidating as it sounds.

    Search on "SHBrowseForFolder VB" in google for tips on this API function:
    Public Declare Function SHBrowseForFolder Lib "shell32.dll" (ByRef lpbi As BrowseInfo) As Long

    It all depends on how much you want to learn and how
    "slick" you want the solution to be.

    I use a home-grown class mod that wraps up the FileSystemObject methods,
    so this code will not work for you, but it shows you a technique for creating
    a list of folders:

    Sub CreateFolderStructure(strDrv As String)
    Dim i As Long
    ReDim arrFolders(9)
    arrFolders(1) = strDrv & ROOTFOLDER
    arrFolders(2) = strDrv & ROOTFOLDER & "Excel"
    arrFolders(3) = strDrv & ROOTFOLDER & "Letters"
    arrFolders(4) = strDrv & ROOTFOLDER & "WordDocs"
    arrFolders(5) = strDrv & ROOTFOLDER & "Memoranda"
    arrFolders(6) = strDrv & ROOTFOLDER & "Time Sheets"
    arrFolders(7) = strDrv & ROOTFOLDER & "Travel Claims"
    arrFolders(8) = strDrv & ROOTFOLDER & "Purchase Reqs"
    arrFolders(9) = strDrv & ROOTFOLDER & "User Templates"
    For i = 1 To UBound(arrFolders)
    If Not oFso.FolderExists(arrFolders(i)) Then oFso.MakeFolder (arrFolders(i))
    Next
    End Sub
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    Re: VB? to Create Folders (Office XP)

    Thanks, Kevin...but this Kevin is "snowed" as I really don't know VB/VBA...so I was hoping there might be some direct (read: simple) approach to this that I could use. Seems sorta strange that WordPerfect pulls this off directly in the product and Word doesn't or can't...so I suspected that WP used VB or some relatively straight-forward approach that enabled a "user-friendly" series of instructions to be written to do this. SIGH.

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VB? to Create Folders (Office XP)

    Can you post the WordPerfect code?

  5. #5
    Star Lounger
    Join Date
    Feb 2003
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB? to Create Folders (Office XP)

    Oh, OK - step by step for the non-coder. There are lots of other things you can do, but here is the quickest to get things happening for your user.

    (1) In Word, press ALT-F11: the VBA environment will open. In the Project pane, right click on Normal and choose Insert Module. A new module will be created, and opened in the main code pane.
    a) Paste the code below into the code pane. Change the sub folder names to whatever you want.
    [img]/forums/images/smilies/cool.gif[/img] Now the tricky bit: use Tools / References, and scroll down the huge long list until you find "Microsoft Scripting Runtime" - put a tick in the box next to it, then OK.
    c) Use File / Close and Return to MIcrosoft Word - save the changes when prompted.
    (2) Now, create toolbar button or a menu item to fire the code. Right click on a menu or toolbar, and choose "Customize". On the Commands tab, choose the category Macro, and you should see our MakeFolders macro in the list. Drag and drop on a toolbar or a menu. Right click on the item to customize how it is displayed - make the name nicer, and choose an icon. Check that Save in: is set to Normal.dot. Close, and exit Word, saving changes.
    (3) Now put your normal.dot onto the user's machine - in Office XP, it needs to go into their templates directory - by default, something like "Cocuments and Settings{user name}Application DataMicrosoftTemplates"

    They will now see the button/menu item, and it will "just work"

    '-----/code starts-----
    Sub MakeFolders()
    'manually make a reference to the Microsoft Scripting Runtime - Tools, References
    Dim fso As New FileSystemObject
    Dim strRootFolder As String
    Dim i As Long
    Const strDrv As String = "C:" 'doh
    ReDim arrFolders(9) 'this is the largest number from the subfolders below

    strRootFolder = InputBox("Please enter the folder name", "Make Folders Utility")

    arrFolders(1) = strDrv & strRootFolder
    arrFolders(2) = strDrv & strRootFolder & "Excel"
    arrFolders(3) = strDrv & strRootFolder & "Letters"
    arrFolders(4) = strDrv & strRootFolder & "WordDocs"
    arrFolders(5) = strDrv & strRootFolder & "Memoranda"
    arrFolders(6) = strDrv & strRootFolder & "Time Sheets"
    arrFolders(7) = strDrv & strRootFolder & "Travel Claims"
    arrFolders(8) = strDrv & strRootFolder & "Purchase Reqs"
    arrFolders(9) = strDrv & strRootFolder & "User Templates"

    For i = 1 To UBound(arrFolders)
    If Not fso.FolderExists(arrFolders(i)) Then fso.CreateFolder (arrFolders(i))
    Next

    End Sub
    '-----/code ends-----

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: VB? to Create Folders (Office XP)

    Well, keep in mind that creating folders is a Windows level operation, even if you're doing it from an application like Word or WordPerfect. Word allows you to program that capability into its macros but you can create a new folder from the File... Save As dialog, so I don't see too much reason to do it in VBA at all unless you actually *want* to program it. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    Re: VB? to Create Folders (Office XP)

    So, how do you/I do this from Word? I need a "program" (or macro) to do this because the users want to mimic what they did before in WordPerfect...run a macro, be prompted for the main folder name and then have that folder created in a specific path with specific sub-folders (about 6 of them) in that folder.

  8. #8
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    Re: VB? to Create Folders (Office XP)

    SteveRose -- WHAT DO YOU DRINK? I'll send you some!! FANTASTIC...Yes, I'm shouting. What a piece of cake to install this. It does exactly what I want (with some editing for sub-folder names, etc.). Thank you VERY much!

  9. #9
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    Re: VB? to Create Folders (Office XP)

    Steve, I get an error if I click the CANCEL button on the dialog box that initially opens.

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VB? to Create Folders (Office XP)

    When a user cancels an InputBox, the string is blank. So to trap that you could add some code:

    <pre>strRootFolder = InputBox("Please enter the folder name", "Make Folders Utility")

    If strRootFolder = vbNullString Then
    MsgBox "Ciao!"
    Exit Sub
    End If
    </pre>

    Obviously you can customize to fit your needs. You might also, at some point, want to validate the input, for example, to stop short if they used illegal punctuation...

  11. #11
    Star Lounger
    Join Date
    Feb 2003
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    53
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: VB? to Create Folders (Office XP)

    I was gonna say
    after the line
    strRootFolder = InputBox("Please enter the folder name", "Make Folders Utility")
    add a new line :
    If Len(strRootFolder) = 0 Then Exit Sub

    But Jeff's is great.

  12. #12
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    La Jolla, CA
    Posts
    1,470
    Thanks
    30
    Thanked 62 Times in 58 Posts

    Re: VB? to Create Folders (Office XP)

    OK, Steve & Jeff...Just GREAT...Guess this means Steve shares one of his pints with Jeff. Thank you both very much. Greatly appreciated (can you tell I'm happy about this?).

  13. #13
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: VB? to Create Folders (Office XP)

    Glad you're liking it. And when you get the next hangover (no programming job is ever done!), come on back. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •