PC Name fro Excel VBA

Please is it possible to return a computer name from Excel VBA? If so how?

In advance , many thanks

Norman Mitchell
NormanMitchellAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ukerandiConnect With a Mentor Commented:
Private Declare Function GetComputerName Lib "kernel32" _
        Alias "GetComputerNameA" ( _
        ByVal lpBuffer As String, _
        ByRef nSize As Long) As Long


Private Sub CommandButton1_Click()

MsgBox CompName()
End Sub




Public Property Get ComputerName() As String

  Dim stBuff As String * 255, lAPIResult As Long
  Dim lBuffLen As Long
 
  lBuffLen = 255
 
  lAPIResult = GetComputerName(stBuff, lBuffLen)
 
  If lBuffLen > 0 Then ComputerName = Left(stBuff, lBuffLen)

End Property
Public Function CompName() As String
  CompName = ComputerName
End Function
0
 
StephenJRCommented:
This might work, it does for me at least:
MsgBox Environ$("computername")

Open in new window

0
 
ukerandiCommented:
MsgBox Environ("computername")
0
 
NormanMitchellAuthor Commented:
Once again many thanks - you guys always come up trumps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.