Consulting

Results 1 to 16 of 16

Thread: Solved: Creating new folder & saving copy as:

  1. #1
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location

    Solved: Creating new folder & saving copy as:

    Hi All, hope you can help...

    I think most will see what I'm trying to do here, but the main problem is I dont know how to do create a new folder via VBA:

    [vba]Option Explicit
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    'My default file path is the desktop, so need 2 change it here to C:
    Application.DefaultFilePath = "C:\"

    'now, if it doesn't already exist, I want to create a folder on the c drive
    'labelled with the active workbooks name (but without the .xls)

    'I want to save a COPY of the active book to this folder titled
    'as: ActiveWorkbook.Name & Date + Time
    ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Name & Date + Time '< this gives an error

    'now I want to restore my default file path
    Application.DefaultFilePath = "C:\windows\desktop"

    End Sub[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  2. #2
    VBAX Newbie
    Joined
    Feb 2005
    Posts
    4
    Location
    Hi Jhon,

    You can use the MKDir statement to make a new Directory.If you give


    [vba]Sub MakeNewFolder()
    MkDir "yourfoldername"
    End sub[/vba]

    This will create folder in current working folder .You can specfy full path you want.

    Thanks
    Xcelion

  3. #3
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Hi, Xcelion! Welcome to vbax. I've edited your code to use VBA tags. Check my signature for instructions.
    ~Anne Troy

  4. #4
    VBAX Newbie
    Joined
    Feb 2005
    Posts
    4
    Location
    Thanks DreamBoat

  5. #5
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Hi Xcelion,

    Thanx very much for that (that's one step done) I now have it as:[vba]Sub TryIt()
    MkDir ("C:\MyFolder")
    End Sub[/vba]
    which is fine if I have a fixed name for the folder, but it won't allow me to assign a variable name to it.

    Regards,
    John
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  6. #6
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    No clue if this helps or not, John:


    [vba]
    'Hard coded Path value

    'Path = "\\Server\apps\"

    'Path = "C:\"

    'User inputs Path variable

    Path = Range("B1").Value[/vba]
    ~Anne Troy

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Sorry. Of course you'll uncomment what you'll use...
    ~Anne Troy

  8. #8
    VBAX Newbie
    Joined
    Feb 2005
    Posts
    4
    Location
    I tride out this and it's working

    [VBA]
    Sub test()
    Dim str As String
    str = "C:/YourFOlder"
    MkDir str
    End Sub [/VBA]

    Are you facing any problem ?

  9. #9
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    OK thanx again Xcelion,

    This is similar, and it's what I was trying before and it gave error messages, now, all of a sudden it's working? Obviously something wrong with my machine [vba]Sub test1()
    Dim NameIt$
    NameIt = "C:/" & ActiveWorkbook.Name
    MkDir NameIt
    End Sub[/vba]

    @ Dreamboat PS: I found what was wrong before, OE had decided to set itself to "Disconnect after sending & receiving" - again >
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  10. #10
    Now with those questions already answered, how do you get to the my documents of a specific user to create a folder within there if it does not exist.

  11. #11
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    Quote Originally Posted by cleturno
    Now with those questions already answered, how do you get to the my documents of a specific user to create a folder within there if it does not exist.
    Wow, this is an old thread!

    Just change address, e.g.[vba]MkDir "C:\My Documents\" & ActiveWorkbook.Name[/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  12. #12
    Quote Originally Posted by johnske
    Wow, this is an old thread!

    Just change address, e.g.[vba]MkDir "C:\My Documents\" & ActiveWorkbook.Name[/vba]
    This will not work. Sorry I should have been more specific when I asked the question. The platform Win XP Prof. Uses the Windows login name and creates a new folder in the documents and settings folder for each user. So this means that I need a method of finding out who the active windows user is?

  13. #13
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by cleturno
    This will not work. Sorry I should have been more specific when I asked the question. The platform Win XP Prof. Uses the Windows login name and creates a new folder in the documents and settings folder for each user. So this means that I need a method of finding out who the active windows user is?
    Try this
    [vba]

    Dim oWSH As Object
    Const wsh_SPECIAL_FOLDER_MY_DOCS As Long = 16


    Set oWSH = CreateObject("WScript.Shell")


    MsgBox oWSH.SpecialFolders(wsh_SPECIAL_FOLDER_MY_DOCS)

    [/vba]

  14. #14
    Thank you, I think that will take care of it. Made a slight variation, but at least now it will find the correct user dir. Thanks a bunch

  15. #15
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    [VBA]
    Option Explicit
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

    'My default file path is the desktop, so need 2 change it here to C:
    Application.DefaultFilePath = "C:\"

    'now, if it doesn't already exist, I want to create a folder on the c drive
    'labelled with the active workbooks name (but without the .xls)

    'I want to save a COPY of the active book to this folder titled
    'as: ActiveWorkbook.Name & Date + Time
    ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Name & Date + Time '< this gives an error

    'now I want to restore my default file path
    Application.DefaultFilePath = "C:\windows\desktop"

    End Sub
    [/VBA]


    [VBA] ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Name & Date + Time '< this gives an error[/VBA]

    Not to jump in or anything, but wouldn't the default Date and Time formats cause a file name syntax issue causeing the error?

    In addition to the directory issue..addressed above
    alternate"
    [VBA]
    ActiveWorkbook.SaveCopyAs Filename:=ActiveWorkbook.Name & Format(Date,"mm_dd_yy") & format(Time,"hh_mm")
    [/VBA]


    Not sure if this error had been resolved. Hope that helps.

  16. #16
    The problem was getting to the user's mydocs on XP pro based on the user's windows login name. Thanks for the help on that though it is greatly appreciated.

    Thanks,
    Chris

Posting Permissions

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