Adding a username to a print

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
My second post in 2 days!

I've spent some time looking through the Board for some help on this but without much success:

I can add a time and date as a footer on a print - is it possible to add a username as well?(i.e the person logged on to NT and using the sheet). :confused: I tried various options but none worked. Would it work within VBA? or would the required code be rather excessive?

Many thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
Sorry if I'me being a bit thick here but I don't see anything in Print Setup to allow me to add a username.

Thanks.
 

SIXTH SENSE

Well-known Member
Joined
Oct 29, 2003
Messages
1,883

ADVERTISEMENT

:oops:
oh. im sory!
This xcel is already pre-formated. ill check that out!! :oops:
 

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
Hi

Apologies if I didn't explain myself clearly.

I would like to add the username to the footer - the time and date are already there.

Cheers!
 

jimboy

Well-known Member
Joined
Apr 11, 2002
Messages
2,314

ADVERTISEMENT

If you right mouse over the file icon to the left of file and select view code, then paste this in, it will over-ride what you have in the rightfooter, so put the time and date in the left or centre footer...


Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
    With ActiveSheet.PageSetup
        .RightFooter = ""
        .RightFooter = Environ("username")
        
        End With
End Sub
 

Glaswegian

Well-known Member
Joined
Oct 14, 2003
Messages
1,487
thanks jimboy but no success.

Does 'Environ' pick the username from the NT loggon? Is there anything that would do this and add it to the footer? I guess that's what I'm really looking for.

cheers.
 

jimboy

Well-known Member
Joined
Apr 11, 2002
Messages
2,314
Try pasting this code into a normal module and running it (run the username macro), if it brings up a msgbox with your username post back and I'll add it to the before_print code

Code:
Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
                         (ByVal lpName As String, _
                          ByVal lpUserName As String, _
                          lpnLength As Long) As Long

Private Const NO_ERROR = 0
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Sub user_name()
    Dim strBuf As String, lngUser As Long, strUn As String
    strBuf = Space$(255) '//Clear buffer
    lngUser = WNetGetUser("", strBuf, 255)
    If lngUser = NO_ERROR Then
        strUn = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
    MsgBox ("User name is " & Chr(13) & Chr(13) & strUn)
    Else

    End If

End Sub
 

SIXTH SENSE

Well-known Member
Joined
Oct 29, 2003
Messages
1,883
jimboy said:
Try pasting this code into a normal module and running it (run the username macro), if it brings up a msgbox with your username post back and I'll add it to the before_print code

Code:
Option Explicit

Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
                         (ByVal lpName As String, _
                          ByVal lpUserName As String, _
                          lpnLength As Long) As Long

Private Const NO_ERROR = 0
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&

Sub user_name()
    Dim strBuf As String, lngUser As Long, strUn As String
    strBuf = Space$(255) '//Clear buffer
    lngUser = WNetGetUser("", strBuf, 255)
    If lngUser = NO_ERROR Then
        strUn = Left(strBuf, InStr(strBuf, vbNullChar) - 1)
    MsgBox ("User name is " & Chr(13) & Chr(13) & strUn)
    Else

    End If

End Sub


hi Jimboy!
this work great on Xp Prof!!
 

Watch MrExcel Video

Forum statistics

Threads
1,130,168
Messages
5,640,543
Members
417,151
Latest member
ChickenTenderer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top