Excel - How to return the machine name?

Asked By Maury Markowitz on 27-Apr-12 02:15 PM
Hey everyone,

I am back into VBA programming after a long hiatus, and this time I am
on the Mac just to make things confusing and difficult to debug :-)

My current problem is a simple one: we have a DB on a virtual server
and I'd like the VBA to only run when opened on that machine (via
RDP). Google fails to reveal an easy way to test if I am running on a
particular machine - is there one?


GS replied to Maury Markowitz on 27-Apr-12 03:33 PM
Maury Markowitz pretended :

You could try...

If Not Environ("ComputerName") = <ServerName> Then _
ThisWorkbook.Close SaveChanges:=False

..where you substitute your actual server's computer name in the
(obvious) placeholder above.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Ron Rosenfeld replied to Maury Markowitz on 27-Apr-12 03:40 PM
In VBA on the PC, you can get that by a call to the kernel32.dll   I do not know what the equivalent would be on the Mac, though.

====================
Option Explicit
Private Declare Function GetComputerName Lib "kernel32.dll" _
Alias "GetComputerNameA" (ByVal lpBuffer As String, ByRef nSize As Long) As Long

Sub GetName()
Dim lpBuff As String * 1313
GetComputerName lpBuff, Len(lpBuff)
Debug.Print lpBuff
End Sub
===========================
GS replied to Ron Rosenfeld on 27-Apr-12 03:51 PM
Ron,
I gave that 'Mac' issue some thought and is why I opted for VBA's
Environ() function.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
Ron Rosenfeld replied to GS on 27-Apr-12 04:37 PM
I have no experience with the Mac, as I wrote.  But in HELP for the Environ Function with my Excel 2007, it states that that function is not available on the Macintosh.
isabelle replied to GS on 27-Apr-12 05:29 PM
Le 2012-04-27 15:51, GS a ?crit :

yes GS, it is ok on xlxp,

MsgBox Environ("ComputerName")



--
isabelle
isabelle replied to isabelle on 27-Apr-12 05:33 PM
oups,

xlxp(hepl)
Non available for Macintosh.

--
isabelle



Le 2012-04-27 17:29, isabelle a ?crit :
GS replied to Ron Rosenfeld on 27-Apr-12 06:09 PM
It happens that Ron Rosenfeld formulated :

I see that! Hmm.., probably a security thing? I guess it would depend
what OS is on the server the OP uses. His post suggests he is using a
Mac but does not clearly state the OS of the server Excel is running
on. However, I do know Mac users can run Windows on v10 and later...

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
GS replied to GS on 27-Apr-12 06:21 PM
I went to McGimpsey's site to see what I could find there for Mac.
==>nothing! I did see, though, that he runs Windows on his Mac in
Virtual PC.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion
CellShocked replied to GS on 27-Apr-12 09:36 PM
Get the machine name via a terminal or other MAC method.  Assign the
result as a variable name.  Read that variable name from within Excel.
Maury Markowitz replied to GS on 28-Apr-12 08:30 AM
Looking over the thread=85

The machine in question is a PC, a virtual "cloud server" to be exact.
My current model is to place a template file on the server, and ask
the user the open it there. As part of the scripts, it will created
and save out a new file (a simple one, no scripts) that can then be
opened on any Mac or PC.

So I think the Environ method should work for this purpose.

But the purpose of this call is to make sure they open it on the right
machine. If they do not, they get a warning. But I *suspect* that if
they were to open it on the Mac, the compiler would trip on the
Enviorn call and put up some nasty user-unfriendly message. Any ideas
on how to avoid this?
GS replied to Maury Markowitz on 28-Apr-12 12:26 PM
Maury Markowitz formulated on Saturday :

You can use an error handler...

Sub Whatever()
'..Dim stuff

On Error GoTo errhandler 'Macs will choke on this
If Not Environ("ComputerName") = <computername> Then _
ThisWorkbook.Close SaveChanges:=False

'..Do stuff


normalexit:
Exit Sub

errhandler:
MsgBox "You cannot use this template on this machine!"
End Sub

..assumes the stored project runs on the machine accessing the project
on the server.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion