在打开的单元格中显示Excel帐户用户名

Bil*_*ill 6 excel vba ms-office excel-vba

使用Office 2013,我试图插入VBA代码以自动输入员工姓名,因为它在任何Office产品的右上角显示在B2打开excel电子表格的单元格中.我正在使用的当前代码是

Sub Auto_Open()

    Range("B2").Value = " " & Application.UserName

End Sub
Run Code Online (Sandbox Code Playgroud)

但是,这只是使其显示"授权用户".
我究竟做错了什么?

在此输入图像描述

小智 0

尝试这个:

Sub Auto_Open()
Dim Username As String
Dim path As String
Dim sourcefile As String
Dim objFso As FileSystemObject

Set objFso = CreateObject("Scripting.FileSystemObject")

If objFso.FileExists(path & " ~$" & sourcefile) Then
    Username = Split(GetFileOwner(path, " ~$" & sourcefile), "\")(1)
    Range("B2").Value = " " & Username
Else
    MsgBox ("File not Found!")
End If
End Sub
Run Code Online (Sandbox Code Playgroud)