Nil
Nil

Reputation: 11

create new formula using vba excel

Hi im looking for a formulla to show User's full name who is opening excel file.it should show that logged in user name. I tried some VBA script and got succesfull but there is one problem that when i run script that time only it Generate pop up windows saying your user name. it should show user name in cell as a date formulla a"=TODAY()". i have this script please anybody help me to show full user name in cell.

Sub GetUserFullName()
    Dim MyOBJ As Object
    On Error Resume Next
    Set MyOBJ = GetObject("WinMgmts:").instancesOf("Win32_NetworkLoginProfile")
    If Err.Number <> 0 Then
      MsgBox "WMI has not been installed, code will be terminated...", vbExclamation, "Windows Management Instrumentation"
      Exit Sub
    End If
    For Each objItem In MyOBJ
        MyMsg = MyMsg & "Welcome To IT Dept   : " & vbCrLf & vbCrLf & objItem.FullName
    Next
    MsgBox MyMsg, vbInformation, "Swapnil (System Admin)"
End Sub

Upvotes: 1

Views: 347

Answers (1)

Dmitry Pavliv
Dmitry Pavliv

Reputation: 35843


UPD:

Function GetUserFullName() As String
    Dim MyOBJ As Object
    Dim res As String
    On Error Resume Next
    Set MyOBJ = GetObject("WinMgmts:").instancesOf("Win32_NetworkLoginProfile")
    If Err.Number <> 0 Then
      GetUserFullName = "error"
      Exit Function
    End If
    For Each objItem In MyOBJ
        res = res & objItem.FullName
    Next
    GetUserFullName = res
End Function

you can use it in any cell like formula: =GetUserFullName()

Upvotes: 2

Related Questions