Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Excel Tasks
 How to: Display the Current User's ...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Visual Studio Tools for the Microsoft Office System
How to: Display the Current User's Logon ID in a Cell

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

You can read the current user's logon ID using the GetCurrent method of the WindowsIdentity object to represent the current user. You can then display the user's logon ID in a named range on a Microsoft Office Excel 2003 worksheet.

To display the current user's logon ID in a named range

  1. Get the user's logon ID.

    Visual Basic
    Dim user As System.Security.Principal.WindowsIdentity
    user = System.Security.Principal.WindowsIdentity.GetCurrent()
    
    C#
    System.Security.Principal.WindowsIdentity user;
    user = System.Security.Principal.WindowsIdentity.GetCurrent();
    
  2. Create a NamedRange control named userID and display the user's login ID.

    Visual Basic
    Dim userID As Microsoft.Office.Tools.Excel.NamedRange
    userID = Me.Controls.AddNamedRange(Me.Range("A1"), "userID")
    
    userID.Value2 = user.Name
    
    C#
    Microsoft.Office.Tools.Excel.NamedRange userID;
    userID = this.Controls.AddNamedRange(this.Range["A1", missing], "userID");
    
    userID.Value2 = user.Name;
    

Compiling the Code

  • This code must be placed in a sheet class, not in the ThisWorkbook class.

Robust Programming

The following conditions might cause an exception:

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
UserID in Excel Worksheet using PowerShell      Thomas Lee   |   Edit   |   Show History
# new-workbook3.ps1
# Creates a new workbook (with just one sheet), in Excel 2007
# Then we show different cell borders plus add username into a cell
# Thomas Lee - tfl@psp.co.uk


# define enum for line type


$XlLineStyle = @{}
$xllinestyle += @{"xlContinuous" = "1"} # Continuous line.
$xllinestyle += @{"xlDash" = '-4115'} # Dashed line.
$xllinestyle += @{"xlDashDot" = ' 4'} # Alternating dashes and dots.
$xllinestyle += @{"xlDashDotDot" = '5'} # Dash followed by two dots.
$xllinestyle += @{"xlDot" = '-4118'} # Dotted line.
$xllinestyle += @{"xlDouble" = ' -4119'} # Double line.
$xllinestyle += @{"xlLineStyleNone" = ' -4142'} # No line.
$xllinestyle += @{"xlSlantDashDot" = ' 13'} # Slanted dashes.
# Create Excel object
$excel = new-object -comobject Excel.Application
# make Excel visible
$excel.visible = $true
# create a new workbook
$workbook = $excel.workbooks.add()
# default workbook has three sheets, remove 2
# nb: these need to be confirmed in Excel. :-(
$S2 = $workbook.sheets | where {$_.name -eq "Sheet2"}
$s3 = $workbook.sheets | where {$_.name -eq "Sheet3"}
$s3.delete()
$s2.delete()
# get sheet and update sheet name
$s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'}
$s1.name = "PowerShell Sample - Borders"
# set cell values
$s1.range("a1:a1").cells=10
$s1.range("a2:a2").cells=10
$s1.range("a3:A3").cells.formula = "=sum(A1,A2)"
$s1.range("b1:B1").cells=21
$s1.range("b2:b2").cells=21
$s1.range("b3:b3").cells.formula = "=sum(B1,B2)"
$s1.range("D1:D1").cells=2
$s1.range("D2:D2").cells=2
$s1.range("D3:D3").cells.formula = "=sum(d1,d2)"
$S1.range("a5:c5").cells="doodah"
# Give Cell ranges nice nameS
$s1.range("A1:A3").name="Dimsum1"
$s1.range("B1:B3").name="Dimsum2"
$s1.range("d1:d3").name="Dimsum3"
# set different borders
$s1.range("dimsum1").borderaround($xllinestyle.xlContinuous)
$s1.range("dimsum2").borderaround($xllinestyle.xlDashDot)
$s1.range("dimsum3").borderaround($xllinestyle.xldashdotdot)
# Set userid into A7
$uid=[system.security.principal.windowsidentity]::getcurrent()
$rng = $s1.range("A7:A7").cells=$uid.name
 
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker