none
Username Variable RRS feed

  • Question

  • See the below script. Where the %username% is how can i point it to the login name variable?

    I have tried including the environment variable for userprofile but the opentextfile line keeps erroring

    Function ReadAllTextFile()
      
      Const ForReading = 1, ForWriting = 2
      Dim fso, f
      Set fso = CreateObject("Scripting.FileSystemObject")
      Set f = fso.OpenTextFile("c:\users\"%username%"\Temp\email.txt", ForReading)
      ReadAllTextFile = f.ReadAll
      
    End Function

    • Moved by Bill_Stewart Friday, March 15, 2019 6:43 PM This is not "train me in basics step-by-step in a forum" forum
    Tuesday, January 29, 2019 11:48 AM

All replies

  • I have used this in VBScript:

    Option Explicit
    
    Dim objShell, colVars, strName
    
    Set objShell = CreateObject("Wscript.Shell")
    Set colVars = objShell.Environment("Process")
    strName = colVars("USERNAME")
    Wscript.Echo strName
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Tuesday, January 29, 2019 12:05 PM
  • To read a file we do not need to specify an open type.  Just open the file?

    Set f = fso.OpenTextFile(strFilename)

    All things in PowerShell are easier. Your while function becomes this:

    $textFromFile = Get-Content $env:USERPROFILE\Temp\email.txt


    \_(ツ)_/

    Tuesday, January 29, 2019 12:20 PM
  • Again i cant seem to get this to run without errors in the macro in outlook.

    i am Not using power shell 

    Tuesday, January 29, 2019 1:30 PM
  • This is not an Outlook VBA forum.

    The VBScript works correctly on any system assuming folder "temp" exists. From what I see there is no such folder in Windows.

    If you want the users temp folder then use %temp% actually "temp" when using Richard's method.

    strName = colVars("TEMP")


    \_(ツ)_/





    • Edited by jrv Tuesday, January 29, 2019 1:44 PM
    Tuesday, January 29, 2019 1:39 PM