Consulting

Results 1 to 17 of 17

Thread: Make directory at Network Login name

  1. #1

    Make directory at Network Login name

    Dear Friends

    I need your help regarding how to create a directory using Network Login Name.

    I know we can make directory MyDirectory at C:\ using one-line code
    [VBA]MkDir "C:\MyDirectory\"[/VBA]

    But suppose if I want to share my macro-code (in which I need to create directory while running it first time) amongst different users on a network, for example, users A,B and C log in into the network using their login names UsernameA, UsernameB and UsernameC respectively; and they have their network drive with their login name folder H:\Netowrk Username\ , then how can I make MyDirectory in this folder.

    I mean on the network drive,
    H:\Network Username\MyDirectory

    where Network Username would be UsernameA for A, UsernameB for B and UsernameC for C.

    I hope you would understand what I mean to write here.

    Thank you once again.
    Best Regards
    Vishalkumar

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Vishalkumar, i think its something like this:
    [VBA]MkDir "H:\"& Environ("UserName") & \MyDirectory"[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Simon,

    You would need to do it in stages

    [vba]

    MkDir "H:\" & Environ("UserName")
    MkDir "H:\" & Environ("UserName") & "\MyDirectory"
    [/vba]
    Last edited by Bob Phillips; 07-09-2007 at 04:49 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Why the two stages Bob? (nice to hear from you!)
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Becuase it will fail if the parent directory does not already exist.

    Done properly, it should have error handling in case either does/does not exist.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  6. #6
    Hi Simon
    Hello xld

    I tried to use both the alternatives shown here by you both.

    But Compilation error was coming, so I changed it to

    [VBA]MkDir "H:\" & Environ("UserName") & "\MyDirectory"[/VBA]

    by putting extra " after second & sign.
    And then I tried to run it, but it shows error messages, path couldn't find.
    I am wondering how does it recognise different UserNames with this code

    Vishalkumar

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    See my responses explaining this.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    But with this code

    [VBA]MkDir "H:\" & Environ("UserName")
    MkDir "H:\" & Environ("UserName") & "\MyDirectory"[/VBA]

    this error comes:

    [VBA]
    Run time error 75
    Path/file access error[/VBA]

  9. #9
    Hello

    By writing the code in this protection it works:

    [VBA]On Error Resume Next
    MkDir "H:\" & Environ("UserName")
    MkDir "H:\" & Environ("UserName") & "\MyDirectory"
    On Error GoTo 0[/VBA]

    Will it affect the other part of my code, I mean, wherever I will use MyDirectory to store some result or generated files in the code ?

    Thank you so much...


  10. #10
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Nope.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  11. #11
    Thank you once again to both of you

  12. #12
    VBAX Regular
    Joined
    Jul 2007
    Posts
    71
    Location
    Quote Originally Posted by VishalkumarT
    Hello

    By writing the code in this protection it works:

    [vba]On Error Resume Next
    MkDir "H:\" & Environ("UserName")
    MkDir "H:\" & Environ("UserName") & "\MyDirectory"
    On Error GoTo 0[/vba]
    Will it affect the other part of my code, I mean, wherever I will use MyDirectory to store some result or generated files in the code ?

    Thank you so much...

    I would probably do something along these lines:

    [vba]
    If Dir("H:\" & Environ("UserName"), vbDirectory) = "" Then

    MkDir "H:\" & Environ("UserName")
    MkDir "H:\" & Environ("UserName") & "\MyDirectory"

    ElseIf Dir("H:\" & Environ("UserName") & "\MyDirectory", vbDirectory) = "" Then

    MkDir "H:\" & Environ("UserName") & "\MyDirectory"

    Else

    MsgBox "Directories Exist", vbOKOnly

    End If
    [/vba]
    But that's just me. Depends on what you're trying to do.

  13. #13
    Nice things to learn.

    Thanks everybody.

  14. #14
    Dear Friends

    Now I am finding a strange problem (may be for me), I used the trick to find the destination folder on network, using the replies of this post, but i am finind an error, if I have windows/excel of different languages. Some of the network users have English version and some have German version of Excel or office.

    I am using this code with a user defined button to activate it, this is "Custom Button" in English "Benutzerdefinierte Schaltfl?che" in German in the code.

    I mean,

    [VBA]Set mycontrol = mybar.Controls.Item("Custom Button") ' for English version[/VBA]

    or

    [VBA]Set mycontrol = mybar.Controls.Item("Benutzerdefinierte Schaltfl?che") ' for German version[/VBA]

    May I know how I can educate the macro code, that if English version is there then do this and if German version is there then do that ? Though I want to use the same code for If and for Else If part, but just want to perform the action for both of these languages.

    Many thanks for your precious time and help.

    Vishalkumar

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Look at the value of

    [vba]

    application.International(xlcountrysetting)
    [/vba]

    in the German version and test that. UK is 44, I guess US is 01, and Germany should be 49?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  16. #16
    Yes it is 49.

    Thanks.

    Let me check it.

  17. #17
    It is working

    Thank you.

    Best Regards
    Vishalkumar

Posting Permissions

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