Consulting

Results 1 to 4 of 4

Thread: Referencing a user name in a script

  1. #1

    Referencing a user name in a script

    Below is a script which works fine, so whats the problem i hear, well we need to pick up a different file location instead of c:\temp\email.txt. We need to point it too c:\users\%Username%\temp\Email.txt.

    I have tried various environment scripts for user profiles but when ever i add in the results to the script below i have errors, any chance you can point me in the right direction


    Function ReadAllTextFile()
    Const ForReading = 1, ForWriting = 2
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile("c:\Temp\email.txt", ForReading)<<<< change this to c:\users\%Username%\temp\Email.txt
    ReadAllTextFile = f.ReadAll

    End Function


    Public Sub UpdateSubject()

    Dim SaveCode As String
    Dim KeyWord As String
    Dim objItem As MailItem

    KeyWord = "ABC"

    SaveCode = InputBox("Please enter filecode in the format nnn/nnn", "VisualFiles Auto Save", ReadAllTextFile)
    MsgBox SaveCode
    Set objItem = GetCurrentItem()
    objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject


    End Sub
    Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
    Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
    Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
    End Function

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Set f = fso.OpenTextFile("c:\Temp\email.txt", ForReading)<<<< change this to c:\users\%Username%\temp\Email.txt
    1. You used the pipe char | not the backslash \

    2. Try fso.OpenTextFile (ENVIRON("USERPROFILE") & "\temp\email.txt")
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Now getting
    Compile error:
    syntax error

    Function ReadAllTextFile()

    Const ForReading = 1, ForWriting = 2
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fso.OpenTextFile (ENVIRON("USERPROFILE") & "\temp\email.txt")
    ReadAllTextFile = f.ReadAll

    End Function


    Public Sub UpdateSubject()

    Dim SaveCode As String
    Dim KeyWord As String
    Dim objItem As MailItem

    KeyWord = "ABC"

    SaveCode = InputBox("Please enter filecode in the format nnn/nnn", "VisualFiles Auto Save", ReadAllTextFile)
    MsgBox SaveCode
    Set objItem = GetCurrentItem()
    objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject


    End Sub
    Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application

    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
    Case "Explorer"
    Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
    Case "Inspector"
    Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select

    Set objApp = Nothing
    End Function


  4. #4
    My mistake missed the = in the line script thanks works now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •