'Copy Txt from a file and input into script

So we now have the below script. Debug highlights OpenTextfile as highlighted in bold.
Before it is put into the second part of the update script can we confirm somehow that the text has been read.

Function TextFile_PullData()
    'PURPOSE: Send All Data From Text File To A String Variable
    Dim TextFile As Integer
    Dim FilePath As String
    Dim FileContent As String
    Dim strUser As String

    ' get the current user name
    strUser = CreateObject("WScript.Network").UserName
    'or use strUser = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%")

    'File Path of Text File
    FilePath = "C:\Users\" & strUser & "\Temp\VFile.txt"

    'Determine the next file number available for use by the FileOpen function
    TextFile = FreeFile

    'Open the text file
    **Open FilePath For Input As TextFile**

    'Store file content inside a variable
    FileContent = Input(LOF(TextFile), TextFile)

    'Close Text File
    Close TextFile

    'Report Out Text File Contents
    MsgBox FileContent

    'have the function return the data to the calling code
    TextFile_PullData = FileContent
End Function

Sub UpdateSubject()
    Dim SaveCode As String
    Dim KeyWord As String
    Dim objItem As MailItem

    KeyWord = "TSD"

    SaveCode = TextFile_PullData
    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


Solution 1:[1]

This is not VBScript because you are defining your variables As <something>. In VBScript all variables are of type variant.

Anyway, your Sub UpdateSubject may very well read in the content of the text file, but is does nothing other than show it in a messagebox. In order to make it return this data instead of just reading it in a local variable that lives only insife that sub, make it a Function like :

Function TextFile_PullData()
    'PURPOSE: Send All Data From Text File To A String Variable
    Dim TextFile As Integer
    Dim FilePath As String
    Dim FileContent As String
    Dim strUser As string

    ' get the current user name
    strUser = CreateObject("WScript.Network").UserName
    'or use strUser = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERNAME%")

    'File Path of Text File
    FilePath = "C:\Users\" & strUser & "\Temp\VFile.txt"

    'Determine the next file number available for use by the FileOpen function
    TextFile = FreeFile

    'Open the text file
    Open FilePath For Input As TextFile

    'Store file content inside a variable
    FileContent = Input(LOF(TextFile), TextFile)

    'Close Text File
    Close TextFile

    'Report Out Text File Contents
    MsgBox FileContent

    'have the function return the data to the calling code
    TextFile_PullData = FileContent
End Function

Next use that info in the UpdateSubject subroutine

Sub UpdateSubject()
    Dim SaveCode As String
    Dim KeyWord As String
    Dim objItem As MailItem
    Dim FileContent As String

    ' here, you use the function to pull the content of the text file and store it in
    ' a local variable called 'FileContent' to use in your inputbox.
    FileContent = TextFile_PullData


    SaveCode = InputBox("Please enter filecode in the format nnn/nnn", "VisualFiles Auto Save", FileContent)

    Set objItem = GetCurrentItem()
    KeyWord = "TSD"

    objItem.Subject = "[" + KeyWord + "=" + SaveCode + "] " + objItem.Subject

    'or skip the inputox alltogether and set the subject directly:
    'objItem.Subject = "[" + KeyWord + "=" + FileContent + "] " + objItem.Subject
End Sub

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Theo