PC Review


Reply
Thread Tools Rate Thread

Computer Name with VBA

 
 
Eric
Guest
Posts: n/a
 
      22nd Feb 2007
Hello,

I would like my macro to check the name of the computer before running.
How can I retrieve the computer name value through a macro?


Thank you.


Eric


 
Reply With Quote
 
 
 
 
Gary Keramidas
Guest
Posts: n/a
 
      22nd Feb 2007
environ("computername")

--


Gary


"Eric" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello,
>
> I would like my macro to check the name of the computer before running.
> How can I retrieve the computer name value through a macro?
>
>
> Thank you.
>
>
> Eric
>
>



 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      22nd Feb 2007
in case you need some code to show usage, maybe something like this:

Sub get_name()
Dim computername As String
computername = Environ("computername")
MsgBox computername
End Sub


--


Gary


"Eric" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello,
>
> I would like my macro to check the name of the computer before running.
> How can I retrieve the computer name value through a macro?
>
>
> Thank you.
>
>
> Eric
>
>



 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      22nd Feb 2007
in case you need some code to show usage, maybe something like this:

Sub get_name()
Dim computername As String
computername = Environ("computername")
MsgBox computername
End Sub


--


Gary


"Eric" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hello,
>
> I would like my macro to check the name of the computer before running.
> How can I retrieve the computer name value through a macro?
>
>
> Thank you.
>
>
> Eric
>
>



 
Reply With Quote
 
=?Utf-8?B?ZGtpbm4=?=
Guest
Posts: n/a
 
      22nd Feb 2007
Hi Eric

You didn't mention what version of Excel you were using but here is a 32 bit
version that works under most recent version, I haven't tried it under 2007
or Vista but it works under XP

Open a standard module and paste the following code

Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
(ByVal lpBuffer As String, nSize As Long) As Long

Sub Get_Computer_Name_Ex()
Dim Comp_Name_B As String * 25
'this function seems to work differntly than most, it doesn't return the
number of char's placed in the buffer
Ret = GetComputerName(Comp_Name_B, Len(Comp_Name_B))
'but the string is always ended with a null terminated string so we can
use the Chr(0) function to find the end
Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
'and return only the computer name
MsgBox Comp_Name
End Sub

hope this helps

David

"Eric" wrote:

> Hello,
>
> I would like my macro to check the name of the computer before running.
> How can I retrieve the computer name value through a macro?
>
>
> Thank you.
>
>
> Eric
>
>
>

 
Reply With Quote
 
Akash
Guest
Posts: n/a
 
      22nd Feb 2007
On Feb 22, 11:15 am, dkinn <d...@discussions.microsoft.com> wrote:
> Hi Eric
>
> You didn't mention what version of Excel you were using but here is a 32 bit
> version that works under most recent version, I haven't tried it under 2007
> or Vista but it works under XP
>
> Open a standard module and paste the following code
>
> Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
> (ByVal lpBuffer As String, nSize As Long) As Long
>
> Sub Get_Computer_Name_Ex()
> Dim Comp_Name_B As String * 25
> 'this function seems to work differntly than most, it doesn't return the
> number of char's placed in the buffer
> Ret = GetComputerName(Comp_Name_B, Len(Comp_Name_B))
> 'but the string is always ended with a null terminated string so we can
> use the Chr(0) function to find the end
> Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
> 'and return only the computer name
> MsgBox Comp_Name
> End Sub
>
> hope this helps
>
> David
>
> "Eric" wrote:
> > Hello,

>
> > I would like my macro to check the name of the computer before running.
> > How can I retrieve the computer name value through a macro?

>
> > Thank you.

>
> > Eric


its not working Eric, I want to get it print on my Excel Header &
Footer

hws would i do the same

 
Reply With Quote
 
Gary Keramidas
Guest
Posts: n/a
 
      22nd Feb 2007
change the sheet name and range to your needs: just paste this code in a blank
workbbok and it should work, though.

Sub print_name()
Dim ws As Worksheet
Dim computername As String
computername = Environ("computername")
Application.ScreenUpdating = False
Set ws = Worksheets("sheet1")
With ws.PageSetup
.PrintTitleRows = "$1:$1"
.Orientation = xlPortrait
.CenterHeader = "&B&12" & computername
.PrintGridlines = True
.Zoom = 100
.RightMargin = Application.InchesToPoints(0.4)
.LeftMargin = Application.InchesToPoints(0.4)
.TopMargin = Application.InchesToPoints(0.75)
.HeaderMargin = Application.InchesToPoints(0.25)
.BottomMargin = Application.InchesToPoints(0.5)
.RightFooter = "Page " & "&P" & " of " & "&N"
.RightHeader = "&B&12 " & Date
.PrintArea = "A1:G10"
.CenterFooter = ""
.FooterMargin = Application.InchesToPoints(0.3)
.CenterHorizontally = True
End With

ws.PrintPreview

Application.ScreenUpdating = True
End Sub

--


Gary


"Akash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 22, 11:15 am, dkinn <d...@discussions.microsoft.com> wrote:
>> Hi Eric
>>
>> You didn't mention what version of Excel you were using but here is a 32 bit
>> version that works under most recent version, I haven't tried it under 2007
>> or Vista but it works under XP
>>
>> Open a standard module and paste the following code
>>
>> Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
>> (ByVal lpBuffer As String, nSize As Long) As Long
>>
>> Sub Get_Computer_Name_Ex()
>> Dim Comp_Name_B As String * 25
>> 'this function seems to work differntly than most, it doesn't return the
>> number of char's placed in the buffer
>> Ret = GetComputerName(Comp_Name_B, Len(Comp_Name_B))
>> 'but the string is always ended with a null terminated string so we can
>> use the Chr(0) function to find the end
>> Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
>> 'and return only the computer name
>> MsgBox Comp_Name
>> End Sub
>>
>> hope this helps
>>
>> David
>>
>> "Eric" wrote:
>> > Hello,

>>
>> > I would like my macro to check the name of the computer before running.
>> > How can I retrieve the computer name value through a macro?

>>
>> > Thank you.

>>
>> > Eric

>
> its not working Eric, I want to get it print on my Excel Header &
> Footer
>
> hws would i do the same
>



 
Reply With Quote
 
Chip Pearson
Guest
Posts: n/a
 
      22nd Feb 2007
Try the following:

Public Declare Function GetComputerName Lib "kernel32" _
Alias "GetComputerNameA" ( _
ByVal lpBuffer As String, _
nSize As Long) As Long

Sub CompNameToHeaderFooter()
Dim CompName As String
Dim L As Long
CompName = String$(260, vbNullChar)
L = Len(CompName)
GetComputerName CompName, L
CompName = Left(CompName, L)
ActiveSheet.PageSetup.CenterHeader = CompName
ActiveSheet.PageSetup.CenterFooter = CompName
End Sub


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)


"Akash" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 22, 11:15 am, dkinn <d...@discussions.microsoft.com> wrote:
>> Hi Eric
>>
>> You didn't mention what version of Excel you were using but here is a 32
>> bit
>> version that works under most recent version, I haven't tried it under
>> 2007
>> or Vista but it works under XP
>>
>> Open a standard module and paste the following code
>>
>> Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
>> (ByVal lpBuffer As String, nSize As Long) As Long
>>
>> Sub Get_Computer_Name_Ex()
>> Dim Comp_Name_B As String * 25
>> 'this function seems to work differntly than most, it doesn't return
>> the
>> number of char's placed in the buffer
>> Ret = GetComputerName(Comp_Name_B, Len(Comp_Name_B))
>> 'but the string is always ended with a null terminated string so we
>> can
>> use the Chr(0) function to find the end
>> Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
>> 'and return only the computer name
>> MsgBox Comp_Name
>> End Sub
>>
>> hope this helps
>>
>> David
>>
>> "Eric" wrote:
>> > Hello,

>>
>> > I would like my macro to check the name of the computer before running.
>> > How can I retrieve the computer name value through a macro?

>>
>> > Thank you.

>>
>> > Eric

>
> its not working Eric, I want to get it print on my Excel Header &
> Footer
>
> hws would i do the same
>



 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      22nd Feb 2007
It works, thank you.

Eric


"Gary Keramidas" <GKeramidasATmsn.com> wrote in message
news:(E-Mail Removed)...
> in case you need some code to show usage, maybe something like this:
>
> Sub get_name()
> Dim computername As String
> computername = Environ("computername")
> MsgBox computername
> End Sub
>
>
> --
>
>
> Gary
>
>
> "Eric" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hello,
>>
>> I would like my macro to check the name of the computer before running.
>> How can I retrieve the computer name value through a macro?
>>
>>
>> Thank you.
>>
>>
>> Eric
>>
>>

>
>



 
Reply With Quote
 
Eric
Guest
Posts: n/a
 
      22nd Feb 2007
Very nice. Thank you.

Eric


"Gary Keramidas" <GKeramidasATmsn.com> wrote in message
news:%(E-Mail Removed)...
> change the sheet name and range to your needs: just paste this code in a
> blank workbbok and it should work, though.
>
> Sub print_name()
> Dim ws As Worksheet
> Dim computername As String
> computername = Environ("computername")
> Application.ScreenUpdating = False
> Set ws = Worksheets("sheet1")
> With ws.PageSetup
> .PrintTitleRows = "$1:$1"
> .Orientation = xlPortrait
> .CenterHeader = "&B&12" & computername
> .PrintGridlines = True
> .Zoom = 100
> .RightMargin = Application.InchesToPoints(0.4)
> .LeftMargin = Application.InchesToPoints(0.4)
> .TopMargin = Application.InchesToPoints(0.75)
> .HeaderMargin = Application.InchesToPoints(0.25)
> .BottomMargin = Application.InchesToPoints(0.5)
> .RightFooter = "Page " & "&P" & " of " & "&N"
> .RightHeader = "&B&12 " & Date
> .PrintArea = "A1:G10"
> .CenterFooter = ""
> .FooterMargin = Application.InchesToPoints(0.3)
> .CenterHorizontally = True
> End With
>
> ws.PrintPreview
>
> Application.ScreenUpdating = True
> End Sub
>
> --
>
>
> Gary
>
>
> "Akash" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> On Feb 22, 11:15 am, dkinn <d...@discussions.microsoft.com> wrote:
>>> Hi Eric
>>>
>>> You didn't mention what version of Excel you were using but here is a 32
>>> bit
>>> version that works under most recent version, I haven't tried it under
>>> 2007
>>> or Vista but it works under XP
>>>
>>> Open a standard module and paste the following code
>>>
>>> Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA"
>>> (ByVal lpBuffer As String, nSize As Long) As Long
>>>
>>> Sub Get_Computer_Name_Ex()
>>> Dim Comp_Name_B As String * 25
>>> 'this function seems to work differntly than most, it doesn't return
>>> the
>>> number of char's placed in the buffer
>>> Ret = GetComputerName(Comp_Name_B, Len(Comp_Name_B))
>>> 'but the string is always ended with a null terminated string so we
>>> can
>>> use the Chr(0) function to find the end
>>> Comp_Name = Left(Comp_Name_B, InStr(Comp_Name_B, Chr(0)))
>>> 'and return only the computer name
>>> MsgBox Comp_Name
>>> End Sub
>>>
>>> hope this helps
>>>
>>> David
>>>
>>> "Eric" wrote:
>>> > Hello,
>>>
>>> > I would like my macro to check the name of the computer before
>>> > running.
>>> > How can I retrieve the computer name value through a macro?
>>>
>>> > Thank you.
>>>
>>> > Eric

>>
>> its not working Eric, I want to get it print on my Excel Header &
>> Footer
>>
>> hws would i do the same
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Desire advice on Computer Cases Computer Chassis to mount new MSI 975x Motherboard? pattyjamas@hotmail.com DIY PC 7 31st Oct 2006 07:30 PM
Is there a way to synchronize favorites between 2 computers? i.e. copy any favorites on computer A but not computer B to computer B and any on computer B but not computer A to computer A? Huck Microsoft Outlook Contacts 1 17th Sep 2006 10:54 AM
Networking - computer A sees computer B, B cannot see computer A =?Utf-8?B?RGFudw==?= Windows XP Networking 2 13th Jan 2005 03:43 AM
The specified remote computer could not be found. Verify that you have typed the correct computer name or ip address, and then try connecting again. =?Utf-8?B?U3RhY2V5?= Windows XP Work Remotely 3 20th Mar 2004 08:16 PM
Networked 2 computers with cross over cable....able to access files folders and ics on client computer from hostbut not able to access files from client computer on the host computer =?Utf-8?B?SHVnaCBNb3J0ZW5zZW4=?= Windows XP Networking 1 16th Feb 2004 10:29 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:24 AM.