MsgBox Environ("username")
Sub Test_getFullname()
MsgBox getfullname
End Sub
'https://msdn.microsoft.com/en-us/library/aa390386(v=vs.85).aspx#enumerating_wmi_using_vbscript
Function getfullname() As String
Dim objAllNames As Object
On Error Resume Next
Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile")
For Each objIndName In objAllNames
getfullname = objIndName.FullName
Next
End Function
Thank you , I try your code it can get the data which I need.I am not sure what you are after.
The method that you posted would not work for others as you did not include the API function.
If you want the environment variable value for username then:
Code:MsgBox Environ("username")
This will get your full name.
Code:Sub Test_getFullname() MsgBox getfullname End Sub 'https://msdn.microsoft.com/en-us/library/aa390386(v=vs.85).aspx#enumerating_wmi_using_vbscript Function getfullname() As String Dim objAllNames As Object On Error Resume Next Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile") For Each objIndName In objAllNames getfullname = objIndName.FullName Next End Function
@echo off
for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b
echo %DisplayName% > C:\test\fullname.txt
Hi ,Thank you , I try your code it can get the data which I need.
But there is one problem , It took long time (about 30 seconds).
So is there any way faster ?
Function getfullname() As String
Dim objIndName As Object, objAllNames As Object
On Error Resume Next
Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile")
For Each objIndName In objAllNames
getfullname = objIndName.FullName
If getfullname <> "" Then Exit For
Next
End Function
'0.04s
Sub Test_getFullName()
Dim d#, s$
d = Timer
s = getFullName
d = Timer - d
Debug.Print s, d
MsgBox s & " found " & d & " in seconds."
End Sub
Function getFullName() As String
Dim objIndName As Object, objAllNames As Object
On Error Resume Next
Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile")
For Each objIndName In objAllNames
getFullName = objIndName.FullName
If getFullName <> "" Then Exit For
Next
End Function
'0.17s
Sub ws()
Dim s$, d#, a
d = Timer
's = CreateObject("Wscript.Shell").Exec("net user ""%USERNAME%"" /DOMAIN | FIND /I ""Full Name""").StdOut.readall
s = CreateObject("Wscript.Shell").Exec("cmd /c net user ""%USERNAME%"" | FIND /I ""Full Name""").StdOut.readall
s = Replace(Right(s, Len(s) - 29), vbCrLf, "")
d = Timer - d
Debug.Print s, d
MsgBox s & " found " & d & " in seconds."
End Sub
Hi ,
Which macro took 30 seconds to run to completion ?
I ran the macro named Test_getFullname , and it displayed the user name instantaneously.
Narayan
Private Sub CommandButton7_Click()
Call Test_getFullname
End Sub
Sub Test_getFullname()
MsgBox getfullname
End Sub
Function getfullname() As String
Dim objIndName As Object, objAllNames As Object
On Error Resume Next
Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile")
For Each objIndName In objAllNames
getfullname = objIndName.FullName
If getfullname <> "" Then Exit For
Next
End Function
Hi Chihiro,Depends on environment I'd suppose, took about 25 sec first run on my comp.
Then ran much faster on subsequent run, about 2 sec.
Hello Mr Kenneth Hobson,Most tests will usually be longer for the 1st run. In this case, the 3rd run was usually longer for me. For those on big networks, time will be much more.
Method 1 as shown in post #3 and #8:
Somewhat similar to post #5 was somewhat longer. The best test is on a big network but you "may" have to replace the s string's DOMAIN with your domain name. You should be able to get it by Environ("userdomain") or use that in the string as I did for UserName. I can't test that for 6 days though.Code:'0.04s Sub Test_getFullName() Dim d#, s$ d = Timer s = getFullName d = Timer - d Debug.Print s, d MsgBox s & " found " & d & " in seconds." End Sub Function getFullName() As String Dim objIndName As Object, objAllNames As Object On Error Resume Next Set objAllNames = GetObject("Winmgmts:").instancesof("win32_networkloginprofile") For Each objIndName In objAllNames getFullName = objIndName.FullName If getFullName <> "" Then Exit For Next End Function
Code:'0.17s Sub ws() Dim s$, d#, a d = Timer 's = CreateObject("Wscript.Shell").Exec("net user ""%USERNAME%"" /DOMAIN | FIND /I ""Full Name""").StdOut.readall s = CreateObject("Wscript.Shell").Exec("cmd /c net user ""%USERNAME%"" | FIND /I ""Full Name""").StdOut.readall s = Replace(Right(s, Len(s) - 29), vbCrLf, "") d = Timer - d Debug.Print s, d MsgBox s & " found " & d & " in seconds." End Sub
I create and run this bat file , it can work.Not really through vba directly I'd imagine.
Personally, I'd create command line batch file with following script.
Code:@echo off for /f "tokens=2*" %%a in ('net user "%Username%" /domain ^| find /i "Full Name"') do set DisplayName=%%b echo %DisplayName% > C:\test\fullname.txt
Then using Shell() run the batch file and read text file from Excel.