PDA

View Full Version : Referencing a user name in a script



Firemaster
01-29-2019, 08:17 AM
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

Paul_Hossler
01-29-2019, 08:41 AM
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")

Firemaster
01-29-2019, 09:16 AM
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

Firemaster
01-29-2019, 09:30 AM
My mistake missed the = in the line script thanks works now
:hi: