PDA

View Full Version : [SOLVED:] Retrieving initials



RandomGerman
08-05-2018, 10:17 AM
Hi all,

I hope this is a little one ... I have a macro retrieving and showing the User Name and the actual date in a comment field. The keyline of the code is:



shp.TextFrame.TextRange.Characters.Text = Environ("UserName") & " " & DataValue(Now) & ": "


Now I would like to change it a bit, as the User Name sometimes is quite long. I would like to replace it by the initials. But it seems, initials are not part of the information I can retrieve by using "Environ". Any idea how to solve this in a way as simple as the one I got?

Thanks in advance!
RG

Paul_Hossler
08-05-2018, 11:44 AM
Maybe



Option Explicit
Sub Initials()
Dim i As Long
Dim s As String
Dim v As Variant

v = Split(UCase(Environ("USERNAME")), " ")

For i = LBound(v) To UBound(v)
s = s & Left(v(i), 1)
Next i

MsgBox s

End Sub

RandomGerman
08-06-2018, 12:36 AM
Thanks a lot, Paul, this works well and is easy to embed into my code. So I assume, there is no way to read out the initials directly, what I hoped for, as they are part of the general menu in the PowerPoint Options.

Paul_Hossler
08-06-2018, 10:08 AM
I couldn't find the initials

However, if you have a presentation open, you might consider using the build in doc props instead of ENVIRON and then generate the initials




Sub test2()


MsgBox ActivePresentation.BuiltInDocumentProperties("Author")

End Sub

RandomGerman
08-07-2018, 02:01 AM
Thank you, Paul, this works as well. What is the advantage compared to the solution with ENVIRON?

John Wilson
08-07-2018, 02:21 AM
You can read the initials from the registry (Not always the same answer as generating from the author) Author and UserName are not necessarily the same anyway.


Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object
On Error Resume Next
Set myWS = CreateObject("WScript.Shell")
RegKeyRead = myWS.RegRead(i_RegKey)
End Function


Sub chexInitials()
MsgBox RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\UserInitials")
End Sub


Sub chexname()
MsgBox RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\Common\UserInfo\UserName")
End Sub

Paul_Hossler
08-07-2018, 04:41 AM
Thank you, Paul, this works as well. What is the advantage compared to the solution with ENVIRON?

Depends on what you want

1. The person logged on, or

2. The creator of the presentation

RandomGerman
08-08-2018, 07:54 AM
@Paul: Okay, got it. I'm looking for the person logged on. So I have to prefer ENVIRON - or John's solution.

@John: Thank you very much. This seems to be as close as possible to what I was looking for.

Are there any advantages on Paul's first or John's solution, or is it just personal taste to solve it with or without the "external" function?

Thank you both so much!

Paul_Hossler
08-08-2018, 12:37 PM
Since you can put anything in "Initials" even if they don't have anything to do with the User name, John's will retreive them (XYZ in screen shot)

Personally I'd test the length of EVIRON ("UserName") and then if it was too long, use the Split() technique from post 2 to 'shrink' it


So "Some User At Work" would shrink to "S. U. A. Work"

22686

RandomGerman
08-09-2018, 01:53 AM
Ah, I see. While ENVIRON is "incorruptible" using the Log-on name, John's solution is dependent on if the user knows, that he could change initials in the Office options. Very interesting.

Thank you once more!

John Wilson
08-09-2018, 10:33 AM
While it's hard to change Environ(USERNAME) it can be set to anything originally (Mine is johnw which doesn't split to initials.) Depends whether you need the computer logon person or the PowerPoint Author which you use.

RandomGerman
08-10-2018, 12:06 AM
My UserName is only one word, too, but as I never cared about changing my initials in Office, it was only one letter there, too. ;-)

I'm definitely not looking for the author of a presentation but for the person actually working on it. My macro is meant for team communication, that's why I want to add date and initials automatically to the shapes used for this communication. Works fantastic thanks to the help of both of you, I just have to make the decision between the two options, that's why I asked for advantages.

Paul_Hossler
08-11-2018, 05:50 AM
From the #1 --



Now I would like to change it a bit, as the User Name sometimes is quite long. I would like to replace it by the initials. But it seems, initials are not part of the information I can retrieve by using "Environ". Any idea how to solve this in a way as simple as the one I got?


How long is 'quite long'?

The USERNAME is probably the most unambiguous, and if the IT people use real name, it might be the best even if long

RandomGerman
08-16-2018, 01:48 AM
Oh, that's more a philosophical question, maybe. I guess, someone discussing with a person called Abercrombie Jonathan Yellowliver might be less happy with the USERNAME used for the function than someone discussing with Joe Hart. ;-)

I will discuss it with the affected team members, I guess that makes most sense. And it is great to have two options.