Retrieve System Name (Computer Name)

  • Thread starter Thread starter Brian Canes
  • Start date Start date
B

Brian Canes

How do you get the system/computer name of the computer
on which the program is running, within Excel VBA?
Regards to all
Brian
 
Brian said:
How do you get the system/computer name of the computer
on which the program is running, within Excel VBA?

Try this (credits to google groups):

One way:

Sub Network_Information()
Dim sText As String

Set WshNetwork = CreateObject("WScript.Network")
sText = "User = " & WshNetwork.UserName & " / Computer = " &
WshNetwork.ComputerName
MsgBox sText
End Sub

Another way:

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

Sub Test()
MsgBox ReturnComputerName
End Sub

Function ReturnComputerName() As String
Dim NameBuff As String * 256
If GetComputerName(NameBuff, 256) = 1 Then
ReturnComputerName = Left(NameBuff, InStr(NameBuff, Chr(0)) - 1)
End If
End Function

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top