|
-
Sep 4th, 2008, 06:58 AM
#1
Thread Starter
Addicted Member
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.
-
Sep 4th, 2008, 07:12 AM
#2
New Member
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
-
Sep 4th, 2008, 07:16 AM
#3
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
-
Sep 4th, 2008, 07:17 AM
#4
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
-
Sep 4th, 2008, 07:18 AM
#5
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.
-
Sep 4th, 2008, 09:49 AM
#6
Re: How to get current user?
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Sep 9th, 2008, 02:50 AM
#7
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|