+ Reply to Thread
Results 1 to 17 of 17

VBScript and Environ("UserName")

  1. #1
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,441

    VBScript and Environ("UserName")

    .
    Trying to create a VBScript using Environ. All the examples I can locate online are not helpful (actually there are very few examples).

    Here is the script currently used, that includes the USERNAME. I want to use ENVIRON in place of the USERNAME.

    Thanks for any assistance.

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,962

    Re: VBScript and Environ("UserName")

    Please Login or Register  to view this content.
    Ben Van Johnson

  3. #3
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: VBScript and Environ("UserName")

    Environ("USERPROFILE") returns this...

    C:\Users\USERNAME HERE


    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  4. #4
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBScript and Environ("UserName")

    Quote Originally Posted by Logit View Post
    Please Login or Register  to view this content.
    I think you can do it without obtaining the UserName directly. Try changing the red highlighted line of code to this (gets the path to the Desktop directly)...
    Please Login or Register  to view this content.
    Last edited by Rick Rothstein; 03-31-2020 at 02:48 AM.

  5. #5
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBScript and Environ("UserName")

    That’s actually a much better way of doing it, since users can change the location of their desktop

  6. #6
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBScript and Environ("UserName")

    Logit. desktop folder could be moved and the best way to use standard method but not generate path based on username
    Please Login or Register  to view this content.
    Oh it's have already written early :-)

    However for Cureent user and for VBS

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.
    Last edited by BMV; 03-31-2020 at 02:50 AM.

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBScript and Environ("UserName")

    For those who might be interested, here are all of the Windows' special folder paths (that I am aware of) that can be found with the CreateObject that I posted in Message #4 (just substitute the keyword option for the "Desktop" keyword that I used)....

    ' Options For specical folders
    ' AllUsersDesktop
    ' AllUsersStartMenu
    ' AllUsersPrograms
    ' AllUsersStartup
    ' Desktop
    ' Favorites
    ' Fonts
    ' MyDocuments
    ' NetHood
    ' PrintHood
    ' Programs
    ' Recent
    ' SendTo
    ' StartMenu
    ' Startup
    ' Templates

  8. #8
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBScript and Environ("UserName")

    @Rick Rothstein. For me it was always surprise that this list does't include TEMP folder :-)

  9. #9
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,441

    Re: VBScript and Environ("UserName")

    .
    WOW !!!

    Truly amazing resource of information in such a small list ! THANK YOU .. THANK YOU .. THANK YOU to all !

    Precisely what I was looking for.

    You guys are the absolute best !

    Stay healthy my friends.

  10. #10
    Forum Expert Logit's Avatar
    Join Date
    12-23-2012
    Location
    North Carolina
    MS-Off Ver
    Excel 2019 Professional Plus - 2007 Enterprise
    Posts
    7,441

    Re: VBScript and Environ("UserName")

    Quote Originally Posted by Kyle123 View Post
    That’s actually a much better way of doing it, since users can change the location of their desktop
    Interesting ... I never considered that a user might move their desktop. Hmmm ....

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBScript and Environ("UserName")


    Hi Logit !

    For example if the users data are on another local drive than C like for example D,
    and the disk partition was saved after so when Windows crashes the drive C,
    after restoring the backup the users do not lose anything, just have to apply the software updates so
    after such a Windows crash the computer is ready again in less than a hour (w/o the Windows updates) …

  12. #12
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBScript and Environ("UserName")

    Quote Originally Posted by Logit View Post
    Interesting ... I never considered that a user might move their desktop. Hmmm ....
    Windows 10 allow user to move https://www.windowsphoneinfo.com/thr...ndows-10.7267/.
    For enterprise solution folder redirection can be switched on and Desktop, Documents, Download and Favorites folders will be on the network resources.

  13. #13
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBScript and Environ("UserName")

    Quote Originally Posted by BMV View Post
    @Rick Rothstein. For me it was always surprise that this list does't include TEMP folder :-)
    I guess the only way to get at it is something like this...
    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBScript and Environ("UserName")

    Quote Originally Posted by BMV View Post
    Windows 10 allow user to move
    Since Seven, I don't remember for older versions …

  15. #15
    Forum Expert BMV's Avatar
    Join Date
    01-13-2020
    Location
    St-Petersburg, Russia
    MS-Off Ver
    2013-2016
    Posts
    1,329

    Re: VBScript and Environ("UserName")

    [QUOTE=Rick Rothstein;5304145]I guess the only way to get at it is something like this...
    QUOTE] There is anoyher sort way
    Please Login or Register  to view this content.
    Marc L - Yes, You right . I thought about programs moving also and wrote Win10. ( I skipped W8 in my practices and can say nothing about it)
    Last edited by BMV; 03-31-2020 at 02:27 PM.

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBScript and Environ("UserName")


    Yes too for Win 8 as I wrote « since Seven » but I can't remember for older versions than Seven …

  17. #17
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,466

    Re: VBScript and Environ("UserName")

    [QUOTE=BMV;5304186]
    Quote Originally Posted by Rick Rothstein View Post
    I guess the only way to get at it is something like this...
    QUOTE] There is anoyher sort way
    Please Login or Register  to view this content.
    I wasn't aware of the ExpandEnvironmentStrings method, but that may be because it appears VB's Environ function probably uses its underlying code for its functionality. This is a much shorter way to get the Temp folder path (as well as evaluate any other environmental settings)...

    Environ("Temp")

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Protect sheet base on Environ("username")
    By jp16 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-06-2018, 04:49 AM
  2. [SOLVED] VBscript to stop selection at "-----Original Message-----" plus change ";" in the output
    By 20GT in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-01-2017, 11:46 PM
  3. Active Sheet UNLOCKED using Environ("Username")
    By Jarvin24 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-23-2015, 09:57 AM
  4. Problem with Environ("username")
    By Doctorul in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-11-2014, 09:35 AM
  5. Parse first name & surname from Windows logon name (Environ("UserName")
    By mc84excel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-15-2013, 08:08 PM
  6. [SOLVED] Vlookup Environ ("username") to get proper name and allocate protection
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-04-2012, 04:31 AM
  7. Difference between Environ("USERNAME") and Environ$("USERNAME")
    By ron2k_1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-04-2011, 11:15 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1