VBA – what kind of computer am I?

In a previous post I explained I have Excel macros that run in various environments.  One of the things I found I had to check was if a macro was running on a server or in a client workstation – that would help direct the macro to a template location if it was needed.

So – here is some VBA to help determine the type of machine Excel is runing on. Something to note – the VBA is making a call to the WMI scripting library – here is a link to a Microsoft TechNet article providing more background on WMI Scripting.

First, in the General Declaration portion of the module:

Private mvalIsServer As Boolean
Private mvalMultipleOS As Boolean

Private Const mvalWorkStation As Integer = 1
Private Const mvalDomainController As Integer = 2
Private Const mvalServer As Integer = 3

Then the actual function – I wrote it to return a boolean value:

Private Function AmIAServer() As Boolean
On Error GoTo AmIAServer_Err
  Dim ErrorMessage As String
  Dim objOS As Object
  Dim lProductType() As Long
  ErrorMessage = “Error in AmIAServer GetObject Count.  “
  Set objOS = GetObject _
  ReDim lProductType(objOS.Count)
  Dim i As Integer
  i = 0
  ErrorMessage = “Error in AmIAServer GetObject ProductType.  “
  For Each objOS In GetObject _
    lProductType(i) = objOS.ProductType
    i = i + 1
  Set objOS = Nothing
  If i = 0 Then
    mvalMultipleOS = False
    mvalMultipleOS = True
  End If
 ErrorMessage = “Error in AmIAServer Evaluate.  “
 Select Case lProductType(0)
   Case mvalWorkStation
     AmIAServer = False
   Case mvalDomainController
     AmIAServer = True
   Case mvalServer
     AmIAServer = True
   Case Else
     AmIAServer = False
 End Select

Exit Function
    WriteErrorLogEntry ErrorMessage
    AmIAServer = False
End Function


About Lee Greffin
Just another programmer...

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: