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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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.
 
Upvote 0
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!
 
Upvote 0
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0
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!!
 
Upvote 0

Forum statistics

Threads
1,213,482
Messages
6,113,913
Members
448,532
Latest member
9Kimo3

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
Back
Top