Is there anyway to retrieve Registry Values from VBA???

  • Thread starter Thread starter FunkySquid
  • Start date Start date
F

FunkySquid

I'm not sure if this is possible but I am trying to retieve my FULL
Name rather than my logon name from my PC. I was thinking of doing
this through the registry but I'm not sure if this is the best way.
Please help someone, I'm slowly going crazy!
 
It's not in the registry is it?

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
Well I've searched for my full name in the registry and it shows up in
the following string:

HKEY_CURRENT_USER\Software\Inifiles\User Profile.ini\User Profile

"UserName" = "MyFullName"

Is there a way of retrieving this by using VBA or is there another way
to retrieve MyFullName from my computer?
 
Is it the Computer Name that you are after? If so:
Environ("computername")
 
Do you have Administrator priviledges and are you on home or company
workstation? Also are you looking for VBA code to find the name or are you
looking for a way to modify the Registry?
 
I have code on my web site to work with the Registry. See
http://www.cpearson.com/Excel/Registry.htm for the code. I also have an
ActiveX DLL that wraps up all the registry functions in a nice VBA-friendly
package. See http://www.cpearson.com/Excel/registryworx.aspx for info and
free download.

I think the key you are looking for is
HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\UserName

The code to retrieve this, using the RegistryWorx DLL, is shown below. You
can call it from a worksheet cell with =MyFullName() or from other code with

Dim MyName As String
MyName = MyFullName()
If MyName = vbNullString Then
MsgBox "Cannot get name"
Else
MsgBox "Name: " & MyName
End If


Function MyFullName() As String
' requires a Reference to RegistryWorx.DLL
' reads value HKCU\Software\Microsoft\Office\Common\UserInfo\UserName
Dim BaseKey As Long
Dim KeyName As String
Dim ValueName As String
Dim ValueValue As Variant
Dim RW As RegistryWorx.Root

Set RW = New RegistryWorx.Root

BaseKey = &H80000001 ' = HKEY_CURRENT_USER
KeyName = "Software\Microsoft\Office\Common\UserInfo"
ValueName = "UserName"
ValueValue = RW.RegistryGetValue( _
BaseKey:=BaseKey, _
KeyName:=KeyName, _
ValueName:=ValueName)
If IsNull(ValueValue) = True Then
Debug.Print "Cannot read value or value not found: " & vbCrLf & _
"Value Name: " & ValueName & vbCrLf & _
"Key: " & KeyName
MyFullName = vbNullString
Else
MyFullName = ValueValue
End If

End Function


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
I think that will be because of some software that you have installed, I
don't have such a key on my machine.

The problem with any such value is that it relies on the user supplying that
info on some install, something which I wouldn't rely upon.

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
FunkySquid said:
Well I've searched for my full name in the registry and it shows up in
the following string:

HKEY_CURRENT_USER\Software\Inifiles\User Profile.ini\User Profile

"UserName" = "MyFullName"

Is there a way of retrieving this by using VBA or is there another way
to retrieve MyFullName from my computer?

If you need full name of a user account, you can use WinNT provider:

Sub WinNTtest()

Set objNetwork = CreateObject _
("Wscript.Network")

strComputer = objNetwork.ComputerName
'strUser = objNetwork.UserName
strUser = "baba"

Set objUser = GetObject("WinNT://" & _
strComputer & "/" & strUser & ", user")

Debug.Print objUser.get("FullName")

End Sub

If you want full name of a currently logged on user you can use strUser =
objNetwork.UserName to get user's name.

You can also use WMI service:

Sub WMItest()

strUser = "baba"

Set objWMIService = GetObject _
("WinMgmts:root\cimv2")

Set colUsers = objWMIService.ExecQuery _
("Select * From Win32_UserAccount " & _
"Where Name='" & strUser & "'")

For Each objUser In colUsers
Debug.Print objUser.FullName
Next

End Sub


If the user is in a domain you would use LDAP, but I don't have much
experience with that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top