Results 1 to 7 of 7

Thread: How to get current user?

  1. #1

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    How to get current user?

    I would like to thank robdog888 for the code sample posted at http://www.vbforums.com/showthread.p...37#post2445837. Perfect for my current project. My question is related to the said link. I would just like to add a question. How can I save the excel file directly to the Desktop? Is there a way not to hard code the save to file path: example "D:\Test.xls". My application will reside in a Windows 2003 Company Domain environment, is there a way to export the excel file and save directly to the desktop? How can i get the user profile? Thanks.

  2. #2
    New Member
    Join Date
    Sep 2008
    Location
    Bolton, Lancs
    Posts
    6

    Re: How to get current user?

    Hi,

    Item 39 in this thread http://www.vbforums.com/showthread.php?t=342035
    Tells you how to get the current user

    Al

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to get current user?

    environ("userprofile") will return the path to the current user's folder system

    getspecialfolder API call can get the actual desktop folder
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to get current user?

    Code:
    Option Explicit
    
    Private Declare Function SHGetSpecialFolderPath Lib "shell32.dll" Alias _
    "SHGetSpecialFolderPathA" _
    (ByVal hwnd As Long, _
    ByVal pszPath As String, _
    ByVal csidl As Long, _
    ByVal fCreate As Long) As Long
    
    Private Const CSIDL_DESKTOP = &H0
    Private Const MAX_PATH = 260
    
    Private Function GetDesktopFolderPath() As String
        Dim sDesktopPath As String
        sDesktopPath = Space$(MAX_PATH)
        SHGetSpecialFolderPath Me.hwnd, sDesktopPath, CSIDL_DESKTOP, False
        GetDesktopFolderPath = Left$(sDesktopPath, InStr(sDesktopPath, vbNullChar) - 1)
        
    End Function
    
    Private Sub Command1_Click()
    Dim strDesktop As String
    strDesktop = GetDesktopFolderPath
    MsgBox strDesktop
    End Sub

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How to get current user?

    To find the location of special folders (like the Desktop etc), do not even think about the username - as it doesn't actually need to be related to the names of the folders (there is also the issue that the parent folders can be called something else, and/or be on a different drive).

    Instead you should use the API's designed for finding paths, a good example of which can be seen here.


    edit: it seems I was slow, and was last in the rush.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: How to get current user?

    We do have a FAQ item on it (username):
    http://vbforums.com/showthread.php?t=357723
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Addicted Member coolwater's Avatar
    Join Date
    Dec 2004
    Location
    philippines
    Posts
    215

    Re: How to get current user?

    wow lots of replies. thanks. i will try them. I will post if its resolved. thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width