Avatar of Zack
Zack
Flag for Australia asked on

Save Excel File with Specific Computer Name and Username via VBA

Hi EE,


I have series of excel workbooks on Network drives and for the sake of easier cataloging via metadata lookups is their VBA function that will save or make a copy of a workbook assigning specific computer name and username(owner) which can be entered in the VBA code by the user.

Any assistance is appreciated. Using Excel 2013.

Cheers

Thank you.
Microsoft OfficeVBAMicrosoft Excel

Avatar of undefined
Last Comment
Zack

8/22/2022 - Mon
Norie

Zack

You can get the computer and user names using Environ.
Dim strCompName As String
Dim strUserName AS String

    strCompName = Environ("ComputerName")
    strUserName = "Environ("UserName")

Open in new window

Zack

ASKER
Hi Norie,

Tried those commands they don't seem to be coming through over the network may be it's GPO thing. Is there a way you can set the computer and user name via VBA upon saving the workbook.

Thank you.
Norie

Zack

Do you mean set properties of the workbook to reflect the computer/user name?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Roy Cox

Does this work?

Computer name:

Option Explicit

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
                                              (ByVal Buffer As String, _
                                               Size As Long) As Long

Sub GetMachineName()
    Dim strBuf As String * 16, strPcName As String, lngPc As Long

    lngPc = GetComputerName(strBuf, Len(strBuf))
    If lngPc <> 0 Then
        strPcName = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
        MsgBox "Your Name of Computer is :" & strPcName
    Else
        MsgBox "Error"
    End If
End Sub

Open in new window


User name:

Option Explicit


Declare Function GetUserName Lib "advapi32.dll" Alias _
                             "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) _
                             As Long
Sub getUser()


    Dim s As String
    Dim cnt As Long
    Dim dl As Long
    Dim CurUser As String
    cnt = 199
    s = String$(200, 0)
    dl = GetUserName(s, cnt)
    If dl <> 0 Then CurUser = Left$(s, cnt) Else CurUser = ""
    MsgBox CurUser
End Sub

Open in new window

Bill Prew

Tried those commands they don't seem to be coming through over the network

What exactly does that mean?  What values did you get when you accessed those functions, was it real data but not the computer you expected?  Or blank?  Or errors?


Are the users running Excel on a local desktop machine, or is this a virtual desktop configuration?


=========================================================

»bp

ASKER CERTIFIED SOLUTION
Fabrice Lambert

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Zack

ASKER
Hi Bill,

I just get blank results whenever the scripts listed above, secondly yes the environment is entirely virtualised. I haven't tried Fabrice's script yet I will try it tomorrow.

Thank you.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Fabrice Lambert

secondly yes the environment is entirely virtualised
Does that mean users are running Excel on the virtual machine ?
Beware that our solutions will pull data from the computer they are running on, and data pulled from the virtual machine might not be the same as those pulled from a physical one.
Zack

ASKER
Hi Guys,

Fabrice's script works best for most instances which for time being will do :)

Thank you for all your assistance.